]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/discoveryhints.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline into testing
[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
24     static function fromXRD($xrd)
25     {
26         $hints = array();
27
28         foreach ($xrd->links as $link) {
29             switch ($link['rel']) {
30             case Discovery::PROFILEPAGE:
31                 $hints['profileurl'] = $link['href'];
32                 break;
33             case Salmon::NS_REPLIES:
34                 $hints['salmon'] = $link['href'];
35                 break;
36             case Discovery::UPDATESFROM:
37                 $hints['feedurl'] = $link['href'];
38                 break;
39             case Discovery::HCARD:
40                 $hints['hcardurl'] = $link['href'];
41                 break;
42             default:
43                 break;
44             }
45         }
46
47         return $hints;
48     }
49
50     static function fromHcardUrl($url)
51     {
52         $client = new HTTPClient();
53         $client->setHeader('Accept', 'text/html,application/xhtml+xml');
54         $response = $client->get($url);
55
56         if (!$response->isOk()) {
57             return null;
58         }
59
60         return self::hcardHints($response->getBody(),
61                                 $response->getUrl());
62     }
63
64     static function hcardHints($body, $url)
65     {
66         common_debug("starting tidy");
67
68         $body = self::_tidy($body);
69
70         common_debug("done with tidy");
71
72         set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/plugins/OStatus/extlib/hkit/');
73         require_once('hkit.class.php');
74
75         $h      = new hKit;
76
77         $hcards = $h->getByString('hcard', $body);
78
79         if (empty($hcards)) {
80             return array();
81         }
82
83         if (count($hcards) == 1) {
84             $hcard = $hcards[0];
85         } else {
86             foreach ($hcards as $try) {
87                 if (array_key_exists('url', $try)) {
88                     if (is_string($try['url']) && $try['url'] == $url) {
89                         $hcard = $try;
90                         break;
91                     } else if (is_array($try['url'])) {
92                         foreach ($try['url'] as $tryurl) {
93                             if ($tryurl == $url) {
94                                 $hcard = $try;
95                                 break 2;
96                             }
97                         }
98                     }
99                 }
100             }
101             // last chance; grab the first one
102             if (empty($hcard)) {
103                 $hcard = $hcards[0];
104             }
105         }
106
107         $hints = array();
108
109         if (array_key_exists('nickname', $hcard)) {
110             $hints['nickname'] = $hcard['nickname'];
111         }
112
113         if (array_key_exists('fn', $hcard)) {
114             $hints['fullname'] = $hcard['fn'];
115         } else if (array_key_exists('n', $hcard)) {
116             $hints['fullname'] = implode(' ', $hcard['n']);
117         }
118
119         if (array_key_exists('photo', $hcard)) {
120             $hints['avatar'] = $hcard['photo'];
121         }
122
123         if (array_key_exists('note', $hcard)) {
124             $hints['bio'] = $hcard['note'];
125         }
126
127         if (array_key_exists('adr', $hcard)) {
128             if (is_string($hcard['adr'])) {
129                 $hints['location'] = $hcard['adr'];
130             } else if (is_array($hcard['adr'])) {
131                 $hints['location'] = implode(' ', $hcard['adr']);
132             }
133         }
134
135         if (array_key_exists('url', $hcard)) {
136             if (is_string($hcard['url'])) {
137                 $hints['homepage'] = $hcard['url'];
138             } else if (is_array($hcard['url'])) {
139                 // HACK get the last one; that's how our hcards look
140                 $hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
141             }
142         }
143
144         return $hints;
145     }
146
147     private static function _tidy($body)
148     {
149         if (function_exists('tidy_parse_string')) {
150             common_debug("Tidying with extension");
151             $text = tidy_parse_string($body);
152             $text = tidy_clean_repair($text);
153             return $body;
154         } else if ($fullpath = self::_findProgram('tidy')) {
155             common_debug("Tidying with program $fullpath");
156             $tempfile = tempnam('/tmp', 'snht'); // statusnet hcard tidy
157             file_put_contents($tempfile, $source);
158             exec("$fullpath -utf8 -indent -asxhtml -numeric -bare -quiet $tempfile", $tidy);
159             unlink($tempfile);
160             return implode("\n", $tidy);
161         } else {
162             common_debug("Not tidying.");
163             return $body;
164         }
165     }
166
167     private static function _findProgram($name)
168     {
169         $path = $_ENV['PATH'];
170
171         $parts = explode(':', $path);
172
173         foreach ($parts as $part) {
174             $fullpath = $part . '/' . $name;
175             if (is_executable($fullpath)) {
176                 return $fullpath;
177             }
178         }
179
180         return null;
181     }
182 }