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