]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityutils.php
Merge branch 'apinamespace' into 0.9.x
[quix0rs-gnu-social.git] / lib / activityutils.php
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
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Utilities for turning DOMish things into Activityish things
37  *
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.
41  *
42  * @category  OStatus
43  * @package   StatusNet
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/
48  */
49
50 class ActivityUtils
51 {
52     const ATOM = 'http://www.w3.org/2005/Atom';
53
54     const LINK = 'link';
55     const REL  = 'rel';
56     const TYPE = 'type';
57     const HREF = 'href';
58
59     const CONTENT = 'content';
60     const SRC     = 'src';
61
62     /**
63      * Get the permalink for an Activity object
64      *
65      * @param DOMElement $element A DOM element
66      *
67      * @return string related link, if any
68      */
69
70     static function getPermalink($element)
71     {
72         return self::getLink($element, 'alternate', 'text/html');
73     }
74
75     /**
76      * Get the permalink for an Activity object
77      *
78      * @param DOMElement $element A DOM element
79      *
80      * @return string related link, if any
81      */
82
83     static function getLink(DOMNode $element, $rel, $type=null)
84     {
85         $els = $element->childNodes;
86
87         foreach ($els as $link) {
88
89             if (!($link instanceof DOMElement)) {
90                 continue;
91             }
92
93             if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
94
95                 $linkRel = $link->getAttribute(self::REL);
96                 $linkType = $link->getAttribute(self::TYPE);
97
98                 if ($linkRel == $rel &&
99                     (is_null($type) || $linkType == $type)) {
100                     return $link->getAttribute(self::HREF);
101                 }
102             }
103         }
104
105         return null;
106     }
107
108     static function getLinks(DOMNode $element, $rel, $type=null)
109     {
110         $els = $element->childNodes;
111         $out = array();
112
113         foreach ($els as $link) {
114             if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
115
116                 $linkRel = $link->getAttribute(self::REL);
117                 $linkType = $link->getAttribute(self::TYPE);
118
119                 if ($linkRel == $rel &&
120                     (is_null($type) || $linkType == $type)) {
121                     $out[] = $link;
122                 }
123             }
124         }
125
126         return $out;
127     }
128
129     /**
130      * Gets the first child element with the given tag
131      *
132      * @param DOMElement $element   element to pick at
133      * @param string     $tag       tag to look for
134      * @param string     $namespace Namespace to look under
135      *
136      * @return DOMElement found element or null
137      */
138
139     static function child(DOMNode $element, $tag, $namespace=self::ATOM)
140     {
141         $els = $element->childNodes;
142         if (empty($els) || $els->length == 0) {
143             return null;
144         } else {
145             for ($i = 0; $i < $els->length; $i++) {
146                 $el = $els->item($i);
147                 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
148                     return $el;
149                 }
150             }
151         }
152     }
153
154     /**
155      * Grab the text content of a DOM element child of the current element
156      *
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
160      *
161      * @return string content of the child
162      */
163
164     static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
165     {
166         $el = self::child($element, $tag, $namespace);
167
168         if (empty($el)) {
169             return null;
170         } else {
171             return $el->textContent;
172         }
173     }
174
175     static function childHtmlContent(DOMNode $element, $tag, $namespace=self::ATOM)
176     {
177         $el = self::child($element, $tag, $namespace);
178
179         if (empty($el)) {
180             return null;
181         } else {
182             return self::textConstruct($el);
183         }
184     }
185
186     /**
187      * Get the content of an atom:entry-like object
188      *
189      * @param DOMElement $element The element to examine.
190      *
191      * @return string unencoded HTML content of the element, like "This -&lt; is <b>HTML</b>."
192      *
193      * @todo handle remote content
194      * @todo handle embedded XML mime types
195      * @todo handle base64-encoded non-XML and non-text mime types
196      */
197
198     static function getContent($element)
199     {
200         return self::childHtmlContent($element, self::CONTENT, self::ATOM);
201     }
202
203     static function textConstruct($el)
204     {
205         $src  = $el->getAttribute(self::SRC);
206
207         if (!empty($src)) {
208             throw new ClientException(_("Can't handle remote content yet."));
209         }
210
211         $type = $el->getAttribute(self::TYPE);
212
213         // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
214
215         if (empty($type) || $type == 'text') {
216             // We have plaintext saved as the XML text content.
217             // Since we want HTML, we need to escape any special chars.
218             return htmlspecialchars($el->textContent);
219         } else if ($type == 'html') {
220             // We have HTML saved as the XML text content.
221             // No additional processing required once we've got it.
222             $text = $el->textContent;
223             return $text;
224         } else if ($type == 'xhtml') {
225             // Per spec, the <content type="xhtml"> contains a single
226             // HTML <div> with XHTML namespace on it as a child node.
227             // We need to pull all of that <div>'s child nodes and
228             // serialize them back to an (X)HTML source fragment.
229             $divEl = ActivityUtils::child($el, 'div', 'http://www.w3.org/1999/xhtml');
230             if (empty($divEl)) {
231                 return null;
232             }
233             $doc = $divEl->ownerDocument;
234             $text = '';
235             $children = $divEl->childNodes;
236
237             for ($i = 0; $i < $children->length; $i++) {
238                 $child = $children->item($i);
239                 $text .= $doc->saveXML($child);
240             }
241             return trim($text);
242         } else if (in_array($type, array('text/xml', 'application/xml')) ||
243                    preg_match('#(+|/)xml$#', $type)) {
244             throw new ClientException(_("Can't handle embedded XML content yet."));
245         } else if (strncasecmp($type, 'text/', 5)) {
246             return $el->textContent;
247         } else {
248             throw new ClientException(_("Can't handle embedded Base64 content yet."));
249         }
250     }
251
252     /**
253      * Is this a valid URI for remote profile/notice identification?
254      * Does not have to be a resolvable URL.
255      * @param string $uri
256      * @return boolean
257      */
258     static function validateUri($uri)
259     {
260         // Check mailto: URIs first
261
262         if (preg_match('/^mailto:(.*)$/', $uri, $match)) {
263             return Validate::email($match[1], common_config('email', 'check_domain'));
264         }
265
266         if (Validate::uri($uri)) {
267             return true;
268         }
269
270         // Possibly an upstream bug; tag: URIs aren't validated properly
271         // unless you explicitly ask for them. All other schemes are accepted
272         // for basic URI validation without asking.
273         if (Validate::uri($uri, array('allowed_scheme' => array('tag')))) {
274             return true;
275         }
276
277         return false;
278     }
279 }