]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/discoveryhints.php
DiscoveryHints gets microformats2 parsing abilities
[quix0rs-gnu-social.git] / plugins / OStatus / lib / discoveryhints.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Some utilities for generating hint data
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 class DiscoveryHints {
23     static function fromXRD(XML_XRD $xrd)
24     {
25         $hints = array();
26
27         foreach ($xrd->links as $link) {
28             switch ($link->rel) {
29             case WebFingerResource_Profile::PROFILEPAGE:
30                 $hints['profileurl'] = $link->href;
31                 break;
32             case Salmon::REL_SALMON:
33             case Salmon::NS_MENTIONS:   // XXX: deprecated, remove in the future
34             case Salmon::NS_REPLIES:    // XXX: deprecated, remove in the future
35                 $hints['salmon'] = $link->href;
36                 break;
37             case Discovery::UPDATESFROM:
38                 if (empty($link->type) || $link->type == 'application/atom+xml') {
39                     $hints['feedurl'] = $link->href;
40                 }
41                 break;
42             case Discovery::HCARD:
43             case Discovery::MF2_HCARD:
44                 $hints['hcard'] = $link->href;
45                 break;
46             default:
47                 break;
48             }
49         }
50
51         return $hints;
52     }
53
54     static function fromHcardUrl($url)
55     {
56         $client = new HTTPClient();
57         $client->setHeader('Accept', 'text/html,application/xhtml+xml');
58         $response = $client->get($url);
59
60         if (!$response->isOk()) {
61             return null;
62         }
63
64         return self::hcardHints($response->getBody(),
65                                 $response->getUrl());
66     }
67
68     static function hcardHints($body, $url)
69     {
70         $hcard = self::_hcard($body, $url);
71
72         if (empty($hcard)) {
73             return array();
74         }
75
76         $hints = array();
77
78         // XXX: don't copy stuff into an array and then copy it again
79
80         if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) {
81             $hints['nickname'] = $hcard['nickname'][0];
82         }
83
84         if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) {
85             $hints['fullname'] = $hcard['name'][0];
86         }
87
88         if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
89             $hints['avatar'] = $hcard['photo'][0];
90         }
91
92         if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) {
93             $hints['bio'] = $hcard['note'][0];
94         }
95
96         if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) {
97             $hints['location'] = $hcard['adr'][0]['value'];
98         }
99
100         if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) {
101             $hints['homepage'] = $hcard['url'][0];
102         }
103
104         return $hints;
105     }
106
107     static function _hcard($body, $url)
108     {
109         $mf2 = new Mf2\Parser($body, $url);
110         $mf2 = $mf2->parse();
111         
112         if (empty($mf2['items'])) {
113             return null;
114         }
115
116         $hcards = array();
117
118         foreach ($mf2['items'] as $item) {
119             if (!in_array('h-card', $item['type'])) {
120                 continue;
121             }
122
123             // We found a match, return it immediately
124             if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
125                 return $item['properties'];
126             }
127
128             // Let's keep all the hcards for later, to return one of them at least
129             $hcards[] = $item['properties'];
130         }
131
132         // No match immediately for the url we expected, but there were h-cards found
133         if (count($hcards) > 0) {
134             return $hcards[0];
135         }
136
137         return null;
138     }
139 }