]> git.mxchange.org Git - friendica.git/blob - include/Probe.php
Added documentation
[friendica.git] / include / Probe.php
1 <?php
2 /**
3  * @brief This class contain functions for probing URL
4  *
5  */
6
7 use \Friendica\Core\Config;
8 use \Friendica\Core\PConfig;
9
10 require_once("include/feed.php");
11 require_once('include/email.php');
12 require_once('include/network.php');
13
14 class Probe {
15
16         /**
17          * @brief Rearrange the array so that it always has the same order
18          *
19          * @param array $data Unordered data
20          *
21          * @return array Ordered data
22          */
23         private function rearrange_data($data) {
24                 $fields = array("name", "nick", "guid", "url", "addr", "alias",
25                                 "photo", "community", "keywords", "location", "about",
26                                 "batch", "notify", "poll", "request", "confirm", "poco",
27                                 "priority", "network", "pubkey", "baseurl");
28
29                 $newdata = array();
30                 foreach ($fields AS $field)
31                         if (isset($data[$field]))
32                                 $newdata[$field] = $data[$field];
33                         else
34                                 $newdata[$field] = "";
35
36                 // We don't use the "priority" field anymore and replace it with a dummy.
37                 $newdata["priority"] = 0;
38
39                 return $newdata;
40         }
41
42         /**
43          * @brief Probes for XRD data
44          *
45          * @return array
46          *      'lrdd' => Link to LRDD endpoint
47          *      'lrdd-xml' => Link to LRDD endpoint in XML format
48          *      'lrdd-json' => Link to LRDD endpoint in JSON format
49          */
50         private function xrd($host) {
51
52                 $ssl_url = "https://".$host."/.well-known/host-meta";
53                 $url = "http://".$host."/.well-known/host-meta";
54
55                 $xrd_timeout = Config::get('system','xrd_timeout', 20);
56                 $redirects = 0;
57
58                 $xml = fetch_url($ssl_url, false, $redirects, $xrd_timeout, "application/xrd+xml");
59                 $xrd = parse_xml_string($xml, false);
60
61                 if (!is_object($xrd)) {
62                         $xml = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
63                         $xrd = parse_xml_string($xml, false);
64                 }
65                 if (!is_object($xrd))
66                         return false;
67
68                 $links = xml::element_to_array($xrd);
69                 if (!isset($links["xrd"]["link"]))
70                         return false;
71
72                 $xrd_data = array();
73                 foreach ($links["xrd"]["link"] AS $value => $link) {
74                         if (isset($link["@attributes"]))
75                                 $attributes = $link["@attributes"];
76                         elseif ($value == "@attributes")
77                                 $attributes = $link;
78                         else
79                                 continue;
80
81                         if (($attributes["rel"] == "lrdd") AND
82                                 ($attributes["type"] == "application/xrd+xml"))
83                                 $xrd_data["lrdd-xml"] = $attributes["template"];
84                         elseif (($attributes["rel"] == "lrdd") AND
85                                 ($attributes["type"] == "application/json"))
86                                 $xrd_data["lrdd-json"] = $attributes["template"];
87                         elseif ($attributes["rel"] == "lrdd")
88                                 $xrd_data["lrdd"] = $attributes["template"];
89                 }
90                 return $xrd_data;
91         }
92
93         /**
94          * @brief Fetch information about a given uri
95          *
96          * @param string $uri Address that should be probed
97          * @param string $network Test for this specific network
98          * @param integer $uid User ID for the probe (only used for mails)
99          * @param boolean $cache Use cached values?
100          *
101          * @return array uri data
102          */
103         public static function uri($uri, $network = "", $uid = 0, $cache = true) {
104
105                 if ($cache) {
106                         $result = Cache::get("probe_url:".$network.":".$uri);
107                         if (!is_null($result)) {
108                                 $result = unserialize($result);
109                                 return $result;
110                         }
111                 }
112
113                 if ($uid == 0)
114                         $uid = local_user();
115
116                 $data = self::detect($uri, $network, $uid);
117
118                 if (!isset($data["url"]))
119                         $data["url"] = $uri;
120
121                 if ($data["photo"] != "")
122                         $data["baseurl"] = matching_url(normalise_link($data["baseurl"]), normalise_link($data["photo"]));
123                 else
124                         $data["photo"] = App::get_baseurl().'/images/person-175.jpg';
125
126                 if (!isset($data["name"]))
127                         $data["name"] = $data["url"];
128
129                 if (!isset($data["nick"]))
130                         $data["nick"] = strtolower($data["name"]);
131
132                 if (!isset($data["network"]))
133                         $data["network"] = NETWORK_PHANTOM;
134
135                 $data = self::rearrange_data($data);
136
137                 // Only store into the cache if the value seems to be valid
138                 if (!in_array($data['network'], array(NETWORK_PHANTOM, NETWORK_MAIL))) {
139                         Cache::set("probe_url:".$network.":".$uri,serialize($data), CACHE_DAY);
140
141                         /// @todo temporary fix - we need a real contact update function that updates only changing fields
142                         /// The biggest problem is the avatar picture that could have a reduced image size.
143                         /// It should only be updated if the existing picture isn't existing anymore.
144                         if (($data['network'] != NETWORK_FEED) AND ($mode == PROBE_NORMAL) AND
145                                 $data["name"] AND $data["nick"] AND $data["url"] AND $data["addr"] AND $data["poll"])
146                                 q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `url` = '%s', `addr` = '%s',
147                                                 `notify` = '%s', `poll` = '%s', `alias` = '%s', `success_update` = '%s'
148                                         WHERE `nurl` = '%s' AND NOT `self` AND `uid` = 0",
149                                         dbesc($data["name"]),
150                                         dbesc($data["nick"]),
151                                         dbesc($data["url"]),
152                                         dbesc($data["addr"]),
153                                         dbesc($data["notify"]),
154                                         dbesc($data["poll"]),
155                                         dbesc($data["alias"]),
156                                         dbesc(datetime_convert()),
157                                         dbesc(normalise_link($data['url']))
158                         );
159                 }
160                 return $data;
161         }
162
163         /**
164          * @brief Detect information about a given uri
165          *
166          * @param string $uri Address that should be probed
167          * @param string $network Test for this specific network
168          * @param integer $uid User ID for the probe (only used for mails)
169          *
170          * @return array uri data
171          */
172         private function detect($uri, $network, $uid) {
173                 if (strstr($uri, '@')) {
174                         // If the URI starts with "mailto:" then jum directly to the mail detection
175                         if (strpos($url,'mailto:') !== false) {
176                                 $uri = str_replace('mailto:', '', $url);
177                                 return self::mail($uri, $uid);
178                         }
179
180                         if ($network == NETWORK_MAIL)
181                                 return self::mail($uri, $uid);
182
183                         // Remove "acct:" from the URI
184                         $uri = str_replace('acct:', '', $uri);
185
186                         $host = substr($uri,strpos($uri, '@') + 1);
187                         $nick = substr($uri,0, strpos($uri, '@'));
188
189                         $lrdd = self::xrd($host);
190                         if (!$lrdd)
191                                 return self::mail($uri, $uid);
192
193                         $addr = $uri;
194                 } else {
195                         $parts = parse_url($uri);
196                         if (!isset($parts["scheme"]) OR
197                                 !isset($parts["host"]) OR
198                                 !isset($parts["path"]))
199                                 return false;
200
201                         // todo: Ports?
202                         $host = $parts["host"];
203                         $lrdd = self::xrd($host);
204
205                         $path_parts = explode("/", trim($parts["path"], "/"));
206
207                         while (!$lrdd AND (sizeof($path_parts) > 1)) {
208                                 $host .= "/".array_shift($path_parts);
209                                 $lrdd = self::xrd($host);
210                         }
211                         if (!$lrdd)
212                                 return self::feed($uri);
213
214                         $nick = array_pop($path_parts);
215                         $addr = $nick."@".$host;
216                 }
217
218                 $webfinger = false;
219
220                 /// @todo Do we need the prefix "acct:" or "acct://"?
221
222                 foreach ($lrdd AS $key => $link) {
223                         if ($webfinger)
224                                 continue;
225
226                         if (!in_array($key, array("lrdd", "lrdd-xml", "lrdd-json")))
227                                 continue;
228
229                         $path = str_replace('{uri}', urlencode($addr), $link);
230
231                         $webfinger = self::webfinger($path);
232                 }
233                 if (!$webfinger)
234                         return self::feed($uri);
235
236                 $result = false;
237
238                 logger("Probing ".$uri, LOGGER_DEBUG);
239
240                 if (in_array($network, array("", NETWORK_DFRN)))
241                         $result = self::dfrn($webfinger);
242                 if ((!$result AND ($network == "")) OR ($network == NETWORK_DIASPORA))
243                         $result = self::diaspora($webfinger);
244                 if ((!$result AND ($network == "")) OR ($network == NETWORK_OSTATUS))
245                         $result = self::ostatus($webfinger);
246                 if ((!$result AND ($network == "")) OR ($network == NETWORK_PUMPIO))
247                         $result = self::pumpio($webfinger);
248                 if ((!$result AND ($network == "")) OR ($network == NETWORK_FEED))
249                         $result = self::feed($uri);
250                 else {
251                         // We overwrite the detected nick with our try if the previois routines hadn't detected it.
252                         // Additionally it is overwritten when the nickname doesn't make sense (contains spaces).
253                         if (!isset($result["nick"]) OR ($result["nick"] == "") OR (strstr($result["nick"], " ")))
254                                 $result["nick"] = $nick;
255
256                         if (!isset($result["addr"]) OR ($result["addr"] == ""))
257                                 $result["addr"] = $addr;
258                 }
259
260                 logger($uri." is ".$result["network"], LOGGER_DEBUG);
261
262                 if (!isset($result["baseurl"]) OR ($result["baseurl"] == "")) {
263                         $pos = strpos($result["url"], $host);
264                         if ($pos)
265                                 $result["baseurl"] = substr($result["url"], 0, $pos).$host;
266                 }
267
268                 return $result;
269         }
270
271         /**
272          * @brief Do a webfinger request
273          *
274          * @param string $url Address that should be probed
275          *
276          * @return array webfinger data
277          */
278         private function webfinger($url) {
279
280                 $xrd_timeout = Config::get('system','xrd_timeout', 20);
281                 $redirects = 0;
282
283                 $data = fetch_url($url, false, $redirects, $xrd_timeout, "application/xrd+xml");
284                 $xrd = parse_xml_string($data, false);
285
286                 if (!is_object($xrd)) {
287                         // If it is not XML, maybe it is JSON
288                         $webfinger = json_decode($data, true);
289
290                         if (!isset($webfinger["links"]))
291                                 return false;
292
293                         return $webfinger;
294                 }
295
296                 $xrd_arr = xml::element_to_array($xrd);
297                 if (!isset($xrd_arr["xrd"]["link"]))
298                         return false;
299
300                 $webfinger = array();
301
302                 if (isset($xrd_arr["xrd"]["subject"]))
303                         $webfinger["subject"] = $xrd_arr["xrd"]["subject"];
304
305                 if (isset($xrd_arr["xrd"]["alias"]))
306                         $webfinger["aliases"] = $xrd_arr["xrd"]["alias"];
307
308                 $webfinger["links"] = array();
309
310                 foreach ($xrd_arr["xrd"]["link"] AS $value => $data) {
311                         if (isset($data["@attributes"]))
312                                 $attributes = $data["@attributes"];
313                         elseif ($value == "@attributes")
314                                 $attributes = $data;
315                         else
316                                 continue;
317
318                         $webfinger["links"][] = $attributes;
319                 }
320                 return $webfinger;
321         }
322
323         /**
324          * @brief Poll the noscrape page (Friendica specific)
325          *
326          * @param string $noscrape Link to the noscrape page
327          * @param array $data The already fetched data
328          *
329          * @return array noscrape data
330          */
331         private function poll_noscrape($noscrape, $data) {
332                 $content = fetch_url($noscrape);
333                 if (!$content)
334                         return false;
335
336                 $json = json_decode($content, true);
337                 if (!is_array($json))
338                         return false;
339
340                 if (isset($json["fn"]))
341                         $data["name"] = $json["fn"];
342
343                 if (isset($json["addr"]))
344                         $data["addr"] = $json["addr"];
345
346                 if (isset($json["nick"]))
347                         $data["nick"] = $json["nick"];
348
349                 if (isset($json["comm"]))
350                         $data["community"] = $json["comm"];
351
352                 if (isset($json["tags"])) {
353                         $keywords = implode(" ", $json["tags"]);
354                         if ($keywords != "")
355                                 $data["keywords"] = $keywords;
356                 }
357
358                 $location = formatted_location($json);
359                 if ($location)
360                         $data["location"] = $location;
361
362                 if (isset($json["about"]))
363                         $data["about"] = $json["about"];
364
365                 if (isset($json["key"]))
366                         $data["pubkey"] = $json["key"];
367
368                 if (isset($json["photo"]))
369                         $data["photo"] = $json["photo"];
370
371                 if (isset($json["dfrn-request"]))
372                         $data["request"] = $json["dfrn-request"];
373
374                 if (isset($json["dfrn-confirm"]))
375                         $data["confirm"] = $json["dfrn-confirm"];
376
377                 if (isset($json["dfrn-notify"]))
378                         $data["notify"] = $json["dfrn-notify"];
379
380                 if (isset($json["dfrn-poll"]))
381                         $data["poll"] = $json["dfrn-poll"];
382
383                 return $data;
384         }
385
386         /**
387          * @brief Check for valid DFRN data
388          *
389          * @param array $data DFRN data
390          *
391          * @return int Number of errors
392          */
393         public static function valid_dfrn($data) {
394                 $errors = 0;
395                 if(!isset($data['key']))
396                         $errors ++;
397                 if(!isset($data['dfrn-request']))
398                         $errors ++;
399                 if(!isset($data['dfrn-confirm']))
400                         $errors ++;
401                 if(!isset($data['dfrn-notify']))
402                         $errors ++;
403                 if(!isset($data['dfrn-poll']))
404                         $errors ++;
405                 return $errors;
406         }
407
408         /**
409          * @brief Fetch data from a DFRN profile page
410          *
411          * @param string $profile Link to the profile page
412          *
413          * @return array profile data
414          */
415         public static function profile($profile) {
416
417                 $data = array();
418
419                 // Fetch data via noscrape - this is faster
420                 $noscrape = str_replace(array("/hcard/", "/profile/"), "/noscrape/", $profile);
421                 $data = self::poll_noscrape($noscrape, $data);
422
423                 if (!isset($data["notify"]) OR !isset($data["confirm"]) OR
424                         !isset($data["request"]) OR !isset($data["poll"]) OR
425                         !isset($data["poco"]) OR !isset($data["name"]) OR
426                         !isset($data["photo"]))
427                         $data = self::poll_hcard($profile, $data, true);
428
429                 $prof_data = array();
430                 $prof_data["addr"] = $data["addr"];
431                 $prof_data["nick"] = $data["nick"];
432                 $prof_data["dfrn-request"] = $data["request"];
433                 $prof_data["dfrn-confirm"] = $data["confirm"];
434                 $prof_data["dfrn-notify"] = $data["notify"];
435                 $prof_data["dfrn-poll"] = $data["poll"];
436                 $prof_data["dfrn-poco"] = $data["poco"];
437                 $prof_data["photo"] = $data["photo"];
438                 $prof_data["fn"] = $data["name"];
439                 $prof_data["key"] = $data["pubkey"];
440
441                 return $prof_data;
442         }
443
444         /**
445          * @brief Check for DFRN contact
446          *
447          * @param array $webfinger Webfinger data
448          *
449          * @return array DFRN data
450          */
451         private function dfrn($webfinger) {
452
453                 $hcard = "";
454                 $data = array();
455                 foreach ($webfinger["links"] AS $link) {
456                         if (($link["rel"] == NAMESPACE_DFRN) AND ($link["href"] != ""))
457                                 $data["network"] = NETWORK_DFRN;
458                         elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
459                                 $data["poll"] = $link["href"];
460                         elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
461                                 ($link["type"] == "text/html") AND ($link["href"] != ""))
462                                 $data["url"] = $link["href"];
463                         elseif (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
464                                 $hcard = $link["href"];
465                         elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
466                                 $data["poco"] = $link["href"];
467                         elseif (($link["rel"] == "http://webfinger.net/rel/avatar") AND ($link["href"] != ""))
468                                 $data["photo"] = $link["href"];
469
470                         elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
471                                 $data["baseurl"] = trim($link["href"], '/');
472                         elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
473                                 $data["guid"] = $link["href"];
474                         elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
475                                 $data["pubkey"] = base64_decode($link["href"]);
476
477                                 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
478                                 if (strstr($data["pubkey"], 'RSA '))
479                                         $data["pubkey"] = rsatopem($data["pubkey"]);
480                         }
481                 }
482
483                 if (!isset($data["network"]) OR ($hcard == ""))
484                         return false;
485
486                 // Fetch data via noscrape - this is faster
487                 $noscrape = str_replace("/hcard/", "/noscrape/", $hcard);
488                 $data = self::poll_noscrape($noscrape, $data);
489
490                 if (isset($data["notify"]) AND isset($data["confirm"]) AND isset($data["request"]) AND
491                         isset($data["poll"]) AND isset($data["name"]) AND isset($data["photo"]))
492                         return $data;
493
494                 $data = self::poll_hcard($hcard, $data, true);
495
496                 return $data;
497         }
498
499         /**
500          * @brief Poll the hcard page (Diaspora and Friendica specific)
501          *
502          * @param string $hcard Link to the hcard page
503          * @param array $data The already fetched data
504          * @param boolean $dfrn Poll DFRN specific data
505          *
506          * @return array hcard data
507          */
508         private function poll_hcard($hcard, $data, $dfrn = false) {
509
510                 $doc = new DOMDocument();
511                 if (!@$doc->loadHTMLFile($hcard))
512                         return false;
513
514                 $xpath = new DomXPath($doc);
515
516                 $vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
517                 if (!is_object($vcards))
518                         return false;
519
520                 if ($vcards->length == 0)
521                         return false;
522
523                 $vcard = $vcards->item(0);
524
525                 // We have to discard the guid from the hcard in favour of the guid from lrdd
526                 // Reason: Hubzilla doesn't use the value "uid" in the hcard like Diaspora does.
527                 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' uid ')]", $vcard); // */
528                 if (($search->length > 0) AND ($data["guid"] == ""))
529                         $data["guid"] = $search->item(0)->nodeValue;
530
531                 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' nickname ')]", $vcard); // */
532                 if ($search->length > 0)
533                         $data["nick"] = $search->item(0)->nodeValue;
534
535                 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' fn ')]", $vcard); // */
536                 if ($search->length > 0)
537                         $data["name"] = $search->item(0)->nodeValue;
538
539                 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' searchable ')]", $vcard); // */
540                 if ($search->length > 0)
541                         $data["searchable"] = $search->item(0)->nodeValue;
542
543                 $search = $xpath->query("//*[contains(concat(' ', @class, ' '), ' key ')]", $vcard); // */
544                 if ($search->length > 0) {
545                         $data["pubkey"] = $search->item(0)->nodeValue;
546                         if (strstr($data["pubkey"], 'RSA '))
547                                 $data["pubkey"] = rsatopem($data["pubkey"]);
548                 }
549
550                 $search = $xpath->query("//*[@id='pod_location']", $vcard); // */
551                 if ($search->length > 0)
552                         $data["baseurl"] = trim($search->item(0)->nodeValue, "/");
553
554                 $avatar = array();
555                 $photos = $xpath->query("//*[contains(concat(' ', @class, ' '), ' photo ') or contains(concat(' ', @class, ' '), ' avatar ')]", $vcard); // */
556                 foreach ($photos AS $photo) {
557                         $attr = array();
558                         foreach ($photo->attributes as $attribute)
559                                 $attr[$attribute->name] = trim($attribute->value);
560
561                         if (isset($attr["src"]) AND isset($attr["width"]))
562                                 $avatar[$attr["width"]] = $attr["src"];
563                 }
564
565                 if (sizeof($avatar)) {
566                         ksort($avatar);
567                         $data["photo"] = array_pop($avatar);
568                 }
569
570                 if ($dfrn) {
571                         // Poll DFRN specific data
572                         $search = $xpath->query("//link[contains(concat(' ', @rel), ' dfrn-')]");
573                         if ($search->length > 0) {
574                                 foreach ($search AS $link) {
575                                         //$data["request"] = $search->item(0)->nodeValue;
576                                         $attr = array();
577                                         foreach ($link->attributes as $attribute)
578                                                 $attr[$attribute->name] = trim($attribute->value);
579
580                                         $data[substr($attr["rel"], 5)] = $attr["href"];
581                                 }
582                         }
583
584                         // Older Friendica versions had used the "uid" field differently than newer versions
585                         if ($data["nick"] == $data["guid"])
586                                 unset($data["guid"]);
587                 }
588
589
590                 return $data;
591         }
592
593         /**
594          * @brief Check for Diaspora contact
595          *
596          * @param array $webfinger Webfinger data
597          *
598          * @return array Diaspora data
599          */
600         private function diaspora($webfinger) {
601
602                 $hcard = "";
603                 $data = array();
604                 foreach ($webfinger["links"] AS $link) {
605                         if (($link["rel"] == "http://microformats.org/profile/hcard") AND ($link["href"] != ""))
606                                 $hcard = $link["href"];
607                         elseif (($link["rel"] == "http://joindiaspora.com/seed_location") AND ($link["href"] != ""))
608                                 $data["baseurl"] = trim($link["href"], '/');
609                         elseif (($link["rel"] == "http://joindiaspora.com/guid") AND ($link["href"] != ""))
610                                 $data["guid"] = $link["href"];
611                         elseif (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
612                                 ($link["type"] == "text/html") AND ($link["href"] != ""))
613                                 $data["url"] = $link["href"];
614                         elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
615                                 $data["poll"] = $link["href"];
616                         elseif (($link["rel"] == NAMESPACE_POCO) AND ($link["href"] != ""))
617                                 $data["poco"] = $link["href"];
618                         elseif (($link["rel"] == "salmon") AND ($link["href"] != ""))
619                                 $data["notify"] = $link["href"];
620                         elseif (($link["rel"] == "diaspora-public-key") AND ($link["href"] != "")) {
621                                 $data["pubkey"] = base64_decode($link["href"]);
622
623                                 //if (strstr($data["pubkey"], 'RSA ') OR ($link["type"] == "RSA"))
624                                 if (strstr($data["pubkey"], 'RSA '))
625                                         $data["pubkey"] = rsatopem($data["pubkey"]);
626                         }
627                 }
628
629                 if (!isset($data["url"]) OR ($hcard == ""))
630                         return false;
631
632                 if (is_array($webfinger["aliases"]))
633                         foreach ($webfinger["aliases"] AS $alias)
634                                 if (normalise_link($alias) != normalise_link($data["url"]) AND !strstr($alias, "@"))
635                                         $data["alias"] = $alias;
636
637                 // Fetch further information from the hcard
638                 $data = self::poll_hcard($hcard, $data);
639
640                 if (!$data)
641                         return false;
642
643                 if (isset($data["url"]) AND isset($data["guid"]) AND isset($data["baseurl"]) AND
644                         isset($data["pubkey"]) AND ($hcard != "")) {
645                         $data["network"] = NETWORK_DIASPORA;
646
647                         // We have to overwrite the detected value for "notify" since Hubzilla doesn't send it
648                         $data["notify"] = $data["baseurl"]."/receive/users/".$data["guid"];
649                         $data["batch"] = $data["baseurl"]."/receive/public";
650                 } else
651                         return false;
652
653                 return $data;
654         }
655
656         /**
657          * @brief Check for OStatus contact
658          *
659          * @param array $webfinger Webfinger data
660          *
661          * @return array OStatus data
662          */
663         private function ostatus($webfinger) {
664
665                 $pubkey = "";
666                 $data = array();
667                 foreach ($webfinger["links"] AS $link) {
668                         if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
669                                 ($link["type"] == "text/html") AND ($link["href"] != ""))
670                                 $data["url"] = $link["href"];
671                         elseif (($link["rel"] == "salmon") AND ($link["href"] != ""))
672                                 $data["notify"] = $link["href"];
673                         elseif (($link["rel"] == NAMESPACE_FEED) AND ($link["href"] != ""))
674                                 $data["poll"] = $link["href"];
675                         elseif (($link["rel"] == "magic-public-key") AND ($link["href"] != "")) {
676                                 $pubkey = $link["href"];
677
678                                 if (substr($pubkey, 0, 5) === 'data:') {
679                                         if (strstr($pubkey, ','))
680                                                 $pubkey = substr($pubkey, strpos($pubkey, ',') + 1);
681                                         else
682                                                 $pubkey = substr($pubkey, 5);
683                                 } else
684                                         $pubkey = fetch_url($pubkey);
685
686                                 $key = explode(".", $pubkey);
687
688                                 if (sizeof($key) >= 3) {
689                                         $m = base64url_decode($key[1]);
690                                         $e = base64url_decode($key[2]);
691                                         $data["pubkey"] = metopem($m,$e);
692                                 }
693
694                         }
695                 }
696
697                 if (isset($data["notify"]) AND isset($data["pubkey"]) AND
698                         isset($data["poll"]) AND isset($data["url"])) {
699                         $data["network"] = NETWORK_OSTATUS;
700                 } else
701                         return false;
702
703                 // Fetch all additional data from the feed
704                 $feed = fetch_url($data["poll"]);
705                 $feed_data = feed_import($feed,$dummy1,$dummy2, $dummy3, true);
706                 if (!$feed_data)
707                         return false;
708
709                 if ($feed_data["header"]["author-name"] != "")
710                         $data["name"] = $feed_data["header"]["author-name"];
711
712                 if ($feed_data["header"]["author-nick"] != "")
713                         $data["nick"] = $feed_data["header"]["author-nick"];
714
715                 if ($feed_data["header"]["author-avatar"] != "")
716                         $data["photo"] = $feed_data["header"]["author-avatar"];
717
718                 if ($feed_data["header"]["author-id"] != "")
719                         $data["alias"] = $feed_data["header"]["author-id"];
720
721                 // OStatus has serious issues when the the url doesn't fit (ssl vs. non ssl)
722                 // So we take the value that we just fetched, although the other one worked as well
723                 if ($feed_data["header"]["author-link"] != "")
724                         $data["url"] = $feed_data["header"]["author-link"];
725
726                 /// @todo Fetch location and "about" from the feed as well
727                 return $data;
728         }
729
730         /**
731          * @brief Fetch data from a pump.io profile page
732          *
733          * @param string $profile Link to the profile page
734          *
735          * @return array profile data
736          */
737         private function pumpio_profile_data($profile) {
738
739                 $doc = new DOMDocument();
740                 if (!@$doc->loadHTMLFile($profile))
741                         return false;
742
743                 $xpath = new DomXPath($doc);
744
745                 $data = array();
746
747                 // This is ugly - but pump.io doesn't seem to know a better way for it
748                 $data["name"] = trim($xpath->query("//h1[@class='media-header']")->item(0)->nodeValue);
749                 $pos = strpos($data["name"], chr(10));
750                 if ($pos)
751                         $data["name"] = trim(substr($data["name"], 0, $pos));
752
753                 $avatar = $xpath->query("//img[@class='img-rounded media-object']")->item(0);
754                 if ($avatar)
755                         foreach ($avatar->attributes as $attribute)
756                                 if ($attribute->name == "src")
757                                         $data["photo"] = trim($attribute->value);
758
759                 $data["location"] = $xpath->query("//p[@class='location']")->item(0)->nodeValue;
760                 $data["about"] = $xpath->query("//p[@class='summary']")->item(0)->nodeValue;
761
762                 return $data;
763         }
764
765         /**
766          * @brief Check for pump.io contact
767          *
768          * @param array $webfinger Webfinger data
769          *
770          * @return array pump.io data
771          */
772         private function pumpio($webfinger) {
773                 $data = array();
774                 foreach ($webfinger["links"] AS $link) {
775                         if (($link["rel"] == "http://webfinger.net/rel/profile-page") AND
776                                 ($link["type"] == "text/html") AND ($link["href"] != ""))
777                                 $data["url"] = $link["href"];
778                         elseif (($link["rel"] == "activity-inbox") AND ($link["href"] != ""))
779                                 $data["activity-inbox"] = $link["href"];
780                         elseif (($link["rel"] == "activity-outbox") AND ($link["href"] != ""))
781                                 $data["activity-outbox"] = $link["href"];
782                         elseif (($link["rel"] == "dialback") AND ($link["href"] != ""))
783                                 $data["dialback"] = $link["href"];
784                 }
785                 if (isset($data["activity-inbox"]) AND isset($data["activity-outbox"]) AND
786                         isset($data["dialback"]) AND isset($data["url"])) {
787
788                         // by now we use these fields only for the network type detection
789                         // So we unset all data that isn't used at the moment
790                         unset($data["activity-inbox"]);
791                         unset($data["activity-outbox"]);
792                         unset($data["dialback"]);
793
794                         $data["network"] = NETWORK_PUMPIO;
795                 } else
796                         return false;
797
798                 $profile_data = self::pumpio_profile_data($data["url"]);
799
800                 if (!$profile_data)
801                         return false;
802
803                 $data = array_merge($data, $profile_data);
804
805                 return $data;
806         }
807
808         /**
809          * @brief Check page for feed link
810          *
811          * @param string $url Page link
812          *
813          * @return string feed link
814          */
815         private function get_feed_link($url) {
816                 $doc = new DOMDocument();
817
818                 if (!@$doc->loadHTMLFile($url))
819                         return false;
820
821                 $xpath = new DomXPath($doc);
822
823                 //$feeds = $xpath->query("/html/head/link[@type='application/rss+xml']");
824                 $feeds = $xpath->query("/html/head/link[@type='application/rss+xml' and @rel='alternate']");
825                 if (!is_object($feeds))
826                         return false;
827
828                 if ($feeds->length == 0)
829                         return false;
830
831                 $feed_url = "";
832
833                 foreach ($feeds AS $feed) {
834                         $attr = array();
835                         foreach ($feed->attributes as $attribute)
836                         $attr[$attribute->name] = trim($attribute->value);
837
838                         if ($feed_url == "")
839                                 $feed_url = $attr["href"];
840                 }
841
842                 return $feed_url;
843         }
844
845         /**
846          * @brief Check for feed contact
847          *
848          * @param string $url Profile link
849          * @param boolean $probe Do a probe if the page contains a feed link
850          *
851          * @return array feed data
852          */
853         private function feed($url, $probe = true) {
854                 $feed = fetch_url($url);
855                 $feed_data = feed_import($feed, $dummy1, $dummy2, $dummy3, true);
856
857                 if (!$feed_data) {
858                         if (!$probe)
859                                 return false;
860
861                         $feed_url = self::get_feed_link($url);
862
863                         if (!$feed_url)
864                                 return false;
865
866                         return self::feed($feed_url, false);
867                 }
868
869                 if ($feed_data["header"]["author-name"] != "")
870                         $data["name"] = $feed_data["header"]["author-name"];
871
872                 if ($feed_data["header"]["author-nick"] != "")
873                         $data["nick"] = $feed_data["header"]["author-nick"];
874
875                 if ($feed_data["header"]["author-avatar"] != "")
876                         $data["photo"] = $feed_data["header"]["author-avatar"];
877
878                 if ($feed_data["header"]["author-id"] != "")
879                         $data["alias"] = $feed_data["header"]["author-id"];
880
881                 $data["url"] = $url;
882                 $data["poll"] = $url;
883
884                 if ($feed_data["header"]["author-link"] != "")
885                         $data["baseurl"] = $feed_data["header"]["author-link"];
886                 else
887                         $data["baseurl"] = $data["url"];
888
889                 $data["network"] = NETWORK_FEED;
890
891                 return $data;
892         }
893
894         /**
895          * @brief Check for mail contact
896          *
897          * @param string $uri Profile link
898          * @param integer $uid User ID
899          *
900          * @return array mail data
901          */
902         private function mail($uri, $uid) {
903
904                 if (!validate_email($uri))
905                         return false;
906
907                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
908
909                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval($uid));
910
911                 if(count($x) && count($r)) {
912                         $mailbox = construct_mailbox_name($r[0]);
913                         $password = '';
914                         openssl_private_decrypt(hex2bin($r[0]['pass']), $password,$x[0]['prvkey']);
915                         $mbox = email_connect($mailbox,$r[0]['user'], $password);
916                         if(!mbox)
917                                 return false;
918                 }
919
920                 $msgs = email_poll($mbox, $uri);
921                 logger('searching '.$uri.', '.count($msgs).' messages found.', LOGGER_DEBUG);
922
923                 if (!count($msgs))
924                         return false;
925
926                 $data = array();
927
928                 $data["addr"] = $uri;
929                 $data["network"] = NETWORK_MAIL;
930                 $data["name"] = substr($uri, 0, strpos($uri,'@'));
931                 $data["nick"] = $data["name"];
932                 $data["photo"] = avatar_img($uri);
933
934                 $phost = substr($uri, strpos($uri,'@') + 1);
935                 $data["url"] = 'http://'.$phost."/".$data["nick"];
936                 $data["notify"] = 'smtp '.random_string();
937                 $data["poll"] = 'email '.random_string();
938
939                 $x = email_msg_meta($mbox, $msgs[0]);
940                 if(stristr($x[0]->from, $uri))
941                         $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
942                 elseif(stristr($x[0]->to, $uri))
943                         $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
944                 if(isset($adr)) {
945                         foreach($adr as $feadr) {
946                                 if((strcasecmp($feadr->mailbox, $data["name"]) == 0)
947                                         &&(strcasecmp($feadr->host, $phost) == 0)
948                                         && (strlen($feadr->personal))) {
949
950                                         $personal = imap_mime_header_decode($feadr->personal);
951                                         $data["name"] = "";
952                                         foreach($personal as $perspart)
953                                                 if ($perspart->charset != "default")
954                                                         $data["name"] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
955                                                 else
956                                                         $data["name"] .= $perspart->text;
957
958                                         $data["name"] = notags($data["name"]);
959                                 }
960                         }
961                 }
962                 imap_close($mbox);
963
964                 return $data;
965         }
966 }
967 ?>