3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
36 * Utilities for turning DOMish things into Activityish things
38 * Some common functions that I didn't have the bandwidth to try to factor
39 * into some kind of reasonable superclass, so just dumped here. Might
40 * be useful to have an ActivityObject parent class or something.
44 * @author Evan Prodromou <evan@status.net>
45 * @copyright 2010 StatusNet, Inc.
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
47 * @link http://status.net/
51 const ATOM = 'http://www.w3.org/2005/Atom';
58 const CONTENT = 'content';
62 * Get the permalink for an Activity object
64 * @param DOMElement $element A DOM element
66 * @return string related link, if any
68 static function getPermalink(DOMNode $element)
70 return self::getLink($element, 'alternate', 'text/html');
73 static function getSelfLink(DOMNode $element)
75 return self::getLink($element, 'self', 'application/atom+xml');
79 * Get the permalink for an Activity object
81 * @param DOMElement $element A DOM element
83 * @return string related link, if any
85 static function getLink(DOMNode $element, $rel, $type=null)
87 $els = $element->childNodes;
89 foreach ($els as $link) {
90 if (!($link instanceof DOMElement)) {
94 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
95 $linkRel = $link->getAttribute(self::REL);
96 $linkType = $link->getAttribute(self::TYPE);
98 // XXX: Am I allowed to do this according to specs? (matching using common_bare_mime)
99 if ($linkRel == $rel &&
100 (is_null($type) || common_bare_mime($linkType) == common_bare_mime($type))) {
101 return $link->getAttribute(self::HREF);
109 static function getLinks(DOMNode $element, $rel, $type=null)
111 $els = $element->childNodes;
114 for ($i = 0; $i < $els->length; $i++) {
115 $link = $els->item($i);
116 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
117 $linkRel = $link->getAttribute(self::REL);
118 $linkType = $link->getAttribute(self::TYPE);
120 if ($linkRel == $rel &&
121 (is_null($type) || $linkType == $type)) {
131 * Gets the first child element with the given tag
133 * @param DOMElement $element element to pick at
134 * @param string $tag tag to look for
135 * @param string $namespace Namespace to look under
137 * @return DOMElement found element or null
139 static function child(DOMNode $element, $tag, $namespace=self::ATOM)
141 $els = $element->childNodes;
142 if (empty($els) || $els->length == 0) {
145 for ($i = 0; $i < $els->length; $i++) {
146 $el = $els->item($i);
147 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
155 * Gets all immediate child elements with the given tag
157 * @param DOMElement $element element to pick at
158 * @param string $tag tag to look for
159 * @param string $namespace Namespace to look under
161 * @return array found element or null
164 static function children(DOMNode $element, $tag, $namespace=self::ATOM)
168 $els = $element->childNodes;
170 if (!empty($els) && $els->length > 0) {
171 for ($i = 0; $i < $els->length; $i++) {
172 $el = $els->item($i);
173 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
183 * Grab the text content of a DOM element child of the current element
185 * @param DOMElement $element Element whose children we examine
186 * @param string $tag Tag to look up
187 * @param string $namespace Namespace to use, defaults to Atom
189 * @return string content of the child
191 static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
193 $el = self::child($element, $tag, $namespace);
198 return $el->textContent;
202 static function childHtmlContent(DOMNode $element, $tag, $namespace=self::ATOM)
204 $el = self::child($element, $tag, $namespace);
209 return self::textConstruct($el);
214 * Get the content of an atom:entry-like object
216 * @param DOMElement $element The element to examine.
218 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
220 * @todo handle remote content
221 * @todo handle embedded XML mime types
222 * @todo handle base64-encoded non-XML and non-text mime types
224 static function getContent($element)
226 return self::childHtmlContent($element, self::CONTENT, self::ATOM);
229 static function textConstruct($el)
231 $src = $el->getAttribute(self::SRC);
234 // TRANS: Client exception thrown when there is no source attribute.
235 throw new ClientException(_("Can't handle remote content yet."));
238 $type = $el->getAttribute(self::TYPE);
240 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
242 if (empty($type) || $type == 'text') {
243 // We have plaintext saved as the XML text content.
244 // Since we want HTML, we need to escape any special chars.
245 return htmlspecialchars($el->textContent);
246 } else if ($type == 'html') {
247 // We have HTML saved as the XML text content.
248 // No additional processing required once we've got it.
249 $text = $el->textContent;
251 } else if ($type == 'xhtml') {
252 // Per spec, the <content type="xhtml"> contains a single
253 // HTML <div> with XHTML namespace on it as a child node.
254 // We need to pull all of that <div>'s child nodes and
255 // serialize them back to an (X)HTML source fragment.
256 $divEl = ActivityUtils::child($el, 'div', 'http://www.w3.org/1999/xhtml');
260 $doc = $divEl->ownerDocument;
262 $children = $divEl->childNodes;
264 for ($i = 0; $i < $children->length; $i++) {
265 $child = $children->item($i);
266 $text .= $doc->saveXML($child);
269 } else if (in_array($type, array('text/xml', 'application/xml')) ||
270 preg_match('#(+|/)xml$#', $type)) {
271 // TRANS: Client exception thrown when there embedded XML content is found that cannot be processed yet.
272 throw new ClientException(_("Can't handle embedded XML content yet."));
273 } else if (strncasecmp($type, 'text/', 5)) {
274 return $el->textContent;
276 // TRANS: Client exception thrown when base64 encoded content is found that cannot be processed yet.
277 throw new ClientException(_("Can't handle embedded Base64 content yet."));
282 * Is this a valid URI for remote profile/notice identification?
283 * Does not have to be a resolvable URL.
287 static function validateUri($uri)
289 // Check mailto: URIs first
290 $validate = new Validate();
292 if (preg_match('/^mailto:(.*)$/', $uri, $match)) {
293 return $validate->email($match[1], common_config('email', 'check_domain'));
296 if ($validate->uri($uri)) {
300 // Possibly an upstream bug; tag: URIs aren't validated properly
301 // unless you explicitly ask for them. All other schemes are accepted
302 // for basic URI validation without asking.
303 if ($validate->uri($uri, array('allowed_schemes' => array('tag')))) {
310 static function getFeedAuthor($feedEl)
312 // Try old and deprecated activity:subject
314 $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
316 if (!empty($subject)) {
317 return new ActivityObject($subject);
320 // Try the feed author
322 $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
324 if (!empty($author)) {
325 return new ActivityObject($author);
328 // Sheesh. Not a very nice feed! Let's try fingerpoken in the
331 $entries = $feedEl->getElementsByTagNameNS(Activity::ATOM, 'entry');
333 if (!empty($entries) && $entries->length > 0) {
335 $entry = $entries->item(0);
337 // Try the (deprecated) activity:actor
339 $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
341 if (!empty($actor)) {
342 return new ActivityObject($actor);
347 $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
349 if (!empty($author)) {
350 return new ActivityObject($author);
357 static function compareTypes($type, $objects)
359 $type = self::resolveUri($type, false);
360 foreach ((array)$objects as $object) {
361 if ($type === self::resolveUri($object)) {
368 static function compareVerbs($type, $objects)
370 return self::compareTypes($type, $objects);
373 static function resolveUri($uri, $make_relative=false)
376 throw new ServerException('No URI to resolve in ActivityUtils::resolveUri');
379 if (!$make_relative && parse_url($uri, PHP_URL_SCHEME) == '') { // relative -> absolute
380 $uri = Activity::SCHEMA . $uri;
381 } elseif ($make_relative) { // absolute -> relative
382 $uri = basename($uri); //preg_replace('/^http:\/\/activitystrea\.ms\/schema\/1\.0\//', '', $uri);
383 } // absolute schemas pass through unharmed
388 static function findLocalObject(array $uris, $type=ActivityObject::NOTE) {
390 // TODO: Extend this in plugins etc. and describe in EVENTS.txt
391 if (Event::handle('StartFindLocalActivityObject', array($uris, $type, &$obj_class))) {
392 switch (self::resolveUri($type)) {
393 case ActivityObject::PERSON:
394 // GROUP will also be here in due time...
395 $obj_class = 'Profile';
398 $obj_class = 'Notice';
402 $uris = array_unique($uris);
403 foreach ($uris as $uri) {
405 // the exception thrown will cancel before reaching $object
406 $object = call_user_func("{$obj_class}::fromUri", $uri);
408 } catch (UnknownUriException $e) {
409 common_debug('Could not find local activity object from uri: '.$e->object_uri);
412 if (!$object instanceof Managed_DataObject) {
413 throw new ServerException('Could not find any activityobject stored locally with given URIs: '.var_export($uris,true));
415 Event::handle('EndFindLocalActivityObject', array($object->getUri(), $object->getObjectType(), $object));
419 // Check authorship by supplying a Profile as a default and letting plugins
420 // set it to something else if the activity's author is actually someone
421 // else (like with a group or peopletag feed as handled in OStatus).
423 // NOTE: Returned is not necessarily the supplied profile! For example,
424 // the "feed author" may be a group, but the "activity author" is a person!
425 static function checkAuthorship(Activity $activity, Profile $profile)
427 if (Event::handle('CheckActivityAuthorship', array($activity, &$profile))) {
428 // if (empty($activity->actor)), then we generated this Activity ourselves and can trust $profile
430 $actor_uri = $profile->getUri();
432 if (!in_array($actor_uri, array($activity->actor->id, $activity->actor->link))) {
433 // A mismatch between our locally stored URI and the supplied author?
434 // Probably not more than a blog feed or something (with multiple authors or so)
435 // but log it for future inspection.
436 common_log(LOG_WARNING, "Got an actor '{$activity->actor->title}' ({$activity->actor->id}) on single-user feed for " . $actor_uri);
437 } elseif (empty($activity->actor->id)) {
438 // Plain <author> without ActivityStreams actor info.
439 // We'll just ignore this info for now and save the update under the feed's identity.
443 if (!$profile instanceof Profile) {
444 throw new ServerException('Could not get an author Profile for activity');
450 static public function typeToTitle($type)
452 return ucfirst(self::resolveUri($type, true));
455 static public function verbToTitle($verb)
457 return ucfirst(self::resolveUri($verb, true));