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