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/
52 const ATOM = 'http://www.w3.org/2005/Atom';
59 const CONTENT = 'content';
63 * Get the permalink for an Activity object
65 * @param DOMElement $element A DOM element
67 * @return string related link, if any
70 static function getPermalink($element)
72 return self::getLink($element, 'alternate', 'text/html');
76 * Get the permalink for an Activity object
78 * @param DOMElement $element A DOM element
80 * @return string related link, if any
83 static function getLink(DOMNode $element, $rel, $type=null)
85 $els = $element->childNodes;
87 foreach ($els as $link) {
89 if (!($link instanceof DOMElement)) {
93 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
95 $linkRel = $link->getAttribute(self::REL);
96 $linkType = $link->getAttribute(self::TYPE);
98 if ($linkRel == $rel &&
99 (is_null($type) || $linkType == $type)) {
100 return $link->getAttribute(self::HREF);
108 static function getLinks(DOMNode $element, $rel, $type=null)
110 $els = $element->childNodes;
113 foreach ($els as $link) {
114 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
116 $linkRel = $link->getAttribute(self::REL);
117 $linkType = $link->getAttribute(self::TYPE);
119 if ($linkRel == $rel &&
120 (is_null($type) || $linkType == $type)) {
130 * Gets the first child element with the given tag
132 * @param DOMElement $element element to pick at
133 * @param string $tag tag to look for
134 * @param string $namespace Namespace to look under
136 * @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 * Grab the text content of a DOM element child of the current element
157 * @param DOMElement $element Element whose children we examine
158 * @param string $tag Tag to look up
159 * @param string $namespace Namespace to use, defaults to Atom
161 * @return string content of the child
164 static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
166 $el = self::child($element, $tag, $namespace);
171 return $el->textContent;
175 static function childHtmlContent(DOMNode $element, $tag, $namespace=self::ATOM)
177 $el = self::child($element, $tag, $namespace);
182 return self::textConstruct($el);
187 * Get the content of an atom:entry-like object
189 * @param DOMElement $element The element to examine.
191 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
193 * @todo handle remote content
194 * @todo handle embedded XML mime types
195 * @todo handle base64-encoded non-XML and non-text mime types
198 static function getContent($element)
200 return self::childHtmlContent($element, self::CONTENT, self::ATOM);
203 static function textConstruct($el)
205 $src = $el->getAttribute(self::SRC);
208 throw new ClientException(_("Can't handle remote content yet."));
211 $type = $el->getAttribute(self::TYPE);
213 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
215 if (empty($type) || $type == 'text') {
216 return $el->textContent;
217 } else if ($type == 'html') {
218 $text = $el->textContent;
219 return htmlspecialchars_decode($text, ENT_QUOTES);
220 } else if ($type == 'xhtml') {
221 $divEl = ActivityUtils::child($el, 'div', 'http://www.w3.org/1999/xhtml');
225 $doc = $divEl->ownerDocument;
227 $children = $divEl->childNodes;
229 for ($i = 0; $i < $children->length; $i++) {
230 $child = $children->item($i);
231 $text .= $doc->saveXML($child);
234 } else if (in_array($type, array('text/xml', 'application/xml')) ||
235 preg_match('#(+|/)xml$#', $type)) {
236 throw new ClientException(_("Can't handle embedded XML content yet."));
237 } else if (strncasecmp($type, 'text/', 5)) {
238 return $el->textContent;
240 throw new ClientException(_("Can't handle embedded Base64 content yet."));
245 * Is this a valid URI for remote profile/notice identification?
246 * Does not have to be a resolvable URL.
250 static function validateUri($uri)
252 if (Validate::uri($uri)) {
256 // Possibly an upstream bug; tag: URIs aren't validated properly
257 // unless you explicitly ask for them. All other schemes are accepted
258 // for basic URI validation without asking.
259 if (Validate::uri($uri, array('allowed_scheme' => array('tag')))) {