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($element)
70 return self::getLink($element, 'alternate', 'text/html');
74 * Get the permalink for an Activity object
76 * @param DOMElement $element A DOM element
78 * @return string related link, if any
80 static function getLink(DOMNode $element, $rel, $type=null)
82 $els = $element->childNodes;
84 foreach ($els as $link) {
86 if (!($link instanceof DOMElement)) {
90 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
92 $linkRel = $link->getAttribute(self::REL);
93 $linkType = $link->getAttribute(self::TYPE);
95 if ($linkRel == $rel &&
96 (is_null($type) || $linkType == $type)) {
97 return $link->getAttribute(self::HREF);
105 static function getLinks(DOMNode $element, $rel, $type=null)
107 $els = $element->childNodes;
110 foreach ($els as $link) {
111 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
113 $linkRel = $link->getAttribute(self::REL);
114 $linkType = $link->getAttribute(self::TYPE);
116 if ($linkRel == $rel &&
117 (is_null($type) || $linkType == $type)) {
127 * Gets the first child element with the given tag
129 * @param DOMElement $element element to pick at
130 * @param string $tag tag to look for
131 * @param string $namespace Namespace to look under
133 * @return DOMElement found element or null
135 static function child(DOMNode $element, $tag, $namespace=self::ATOM)
137 $els = $element->childNodes;
138 if (empty($els) || $els->length == 0) {
141 for ($i = 0; $i < $els->length; $i++) {
142 $el = $els->item($i);
143 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
151 * Grab the text content of a DOM element child of the current element
153 * @param DOMElement $element Element whose children we examine
154 * @param string $tag Tag to look up
155 * @param string $namespace Namespace to use, defaults to Atom
157 * @return string content of the child
159 static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
161 $el = self::child($element, $tag, $namespace);
166 return $el->textContent;
170 static function childHtmlContent(DOMNode $element, $tag, $namespace=self::ATOM)
172 $el = self::child($element, $tag, $namespace);
177 return self::textConstruct($el);
182 * Get the content of an atom:entry-like object
184 * @param DOMElement $element The element to examine.
186 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
188 * @todo handle remote content
189 * @todo handle embedded XML mime types
190 * @todo handle base64-encoded non-XML and non-text mime types
192 static function getContent($element)
194 return self::childHtmlContent($element, self::CONTENT, self::ATOM);
197 static function textConstruct($el)
199 $src = $el->getAttribute(self::SRC);
202 // TRANS: Client exception thrown when there is no source attribute.
203 throw new ClientException(_("Can't handle remote content yet."));
206 $type = $el->getAttribute(self::TYPE);
208 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
210 if (empty($type) || $type == 'text') {
211 // We have plaintext saved as the XML text content.
212 // Since we want HTML, we need to escape any special chars.
213 return htmlspecialchars($el->textContent);
214 } else if ($type == 'html') {
215 // We have HTML saved as the XML text content.
216 // No additional processing required once we've got it.
217 $text = $el->textContent;
219 } else if ($type == 'xhtml') {
220 // Per spec, the <content type="xhtml"> contains a single
221 // HTML <div> with XHTML namespace on it as a child node.
222 // We need to pull all of that <div>'s child nodes and
223 // serialize them back to an (X)HTML source fragment.
224 $divEl = ActivityUtils::child($el, 'div', 'http://www.w3.org/1999/xhtml');
228 $doc = $divEl->ownerDocument;
230 $children = $divEl->childNodes;
232 for ($i = 0; $i < $children->length; $i++) {
233 $child = $children->item($i);
234 $text .= $doc->saveXML($child);
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;
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."));
250 * Is this a valid URI for remote profile/notice identification?
251 * Does not have to be a resolvable URL.
255 static function validateUri($uri)
257 // Check mailto: URIs first
259 if (preg_match('/^mailto:(.*)$/', $uri, $match)) {
260 return Validate::email($match[1], common_config('email', 'check_domain'));
263 if (Validate::uri($uri)) {
267 // Possibly an upstream bug; tag: URIs aren't validated properly
268 // unless you explicitly ask for them. All other schemes are accepted
269 // for basic URI validation without asking.
270 if (Validate::uri($uri, array('allowed_scheme' => array('tag')))) {