]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MentionURL/lib/util.php
Move the functionality to a plugin
[quix0rs-gnu-social.git] / plugins / MentionURL / lib / util.php
1 <?php
2
3 function mention_url_representative_hcard($url, $fn=null, $mf2=null) {
4    if(!$mf2) {
5        $request = HTTPClient::start();
6
7         try {
8             $response = $request->get($url);
9         } catch(Exception $ex) {
10             return null;
11         }
12
13         $url = $response->getEffectiveUrl();
14         $mf2 = new Mf2\Parser($response->getBody(), $url);
15         $mf2 = $mf2->parse();
16     }
17
18     $hcard = null;
19
20     if(!empty($mf2['items'])) {
21         $hcards = array();
22         foreach($mf2['items'] as $item) {
23             if(!in_array('h-card', $item['type'])) {
24                 continue;
25             }
26
27             // We found a match, return it immediately
28             if(isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
29                 $hcard = $item['properties'];
30                 break;
31             }
32
33             // Let's keep all the hcards for later, to return one of them at least
34             $hcards[] = $item['properties'];
35         }
36
37         // No match immediately for the url we expected, but there were h-cards found
38         if (count($hcards) > 0) {
39             $hcard = $hcards[0];
40         }
41     }
42
43     if(!$hcard && $fn) {
44         $hcard = array('name' => array($fn));
45     }
46
47     if(!$hcard && $response) {
48         preg_match('/<title>([^<]+)/', $response->getBody(), $match);
49         $hcard = array('name' => array($match[1]));
50     }
51
52     if($hcard && !$hcard['url']) {
53         $hcard['url'] = array($url);
54     }
55
56     return $hcard;
57 }