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