]> git.mxchange.org Git - friendica.git/blob - include/Scrape.php
Merge pull request #1318 from annando/1501-global-contacts
[friendica.git] / include / Scrape.php
1 <?php
2
3 require_once('library/HTML5/Parser.php');
4 require_once('include/crypto.php');
5
6 if(! function_exists('scrape_dfrn')) {
7 function scrape_dfrn($url) {
8
9         $a = get_app();
10
11         $ret = array();
12
13         logger('scrape_dfrn: url=' . $url);
14
15         $s = fetch_url($url);
16
17         if(! $s)
18                 return $ret;
19
20         $headers = $a->get_curl_headers();
21         logger('scrape_dfrn: headers=' . $headers, LOGGER_DEBUG);
22
23
24         $lines = explode("\n",$headers);
25         if(count($lines)) {
26                 foreach($lines as $line) {
27                         // don't try and run feeds through the html5 parser
28                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
29                                 return ret;
30                 }
31         }
32
33         try {
34                 $dom = HTML5_Parser::parse($s);
35         } catch (DOMException $e) {
36                 logger('scrape_dfrn: parse error: ' . $e);
37         }
38
39         if(! $dom)
40                 return $ret;
41
42         $items = $dom->getElementsByTagName('link');
43
44         // get DFRN link elements
45
46         foreach($items as $item) {
47                 $x = $item->getAttribute('rel');
48                 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
49                         $ret['feed_atom'] = $item->getAttribute('href');
50                 if(substr($x,0,5) == "dfrn-") {
51                         $ret[$x] = $item->getAttribute('href');
52                 }
53                 if($x === 'lrdd') {
54                         $decoded = urldecode($item->getAttribute('href'));
55                         if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
56                                 $ret['nick'] = $matches[1];
57                 }
58         }
59
60         // Pull out hCard profile elements
61
62         $largest_photo = 0;
63
64         $items = $dom->getElementsByTagName('*');
65         foreach($items as $item) {
66                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
67                         $level2 = $item->getElementsByTagName('*');
68                         foreach($level2 as $x) {
69                                 if(attribute_contains($x->getAttribute('class'),'fn')) {
70                                         $ret['fn'] = $x->textContent;
71                                 }
72                                 if((attribute_contains($x->getAttribute('class'),'photo'))
73                                         || (attribute_contains($x->getAttribute('class'),'avatar'))) {
74                                         $size = intval($x->getAttribute('width'));
75                                         // dfrn prefers 175, so if we find this, we set largest_size so it can't be topped.
76                                         if(($size > $largest_photo) || ($size == 175) || (! $largest_photo)) {
77                                                 $ret['photo'] = $x->getAttribute('src');
78                                                 $largest_photo = (($size == 175) ? 9999 : $size);
79                                         }
80                                 }
81                                 if(attribute_contains($x->getAttribute('class'),'key')) {
82                                         $ret['key'] = $x->textContent;
83                                 }
84                         }
85                 }
86         }
87
88         return $ret;
89 }}
90
91
92
93
94
95
96 if(! function_exists('validate_dfrn')) {
97 function validate_dfrn($a) {
98         $errors = 0;
99         if(! x($a,'key'))
100                 $errors ++;
101         if(! x($a,'dfrn-request'))
102                 $errors ++;
103         if(! x($a,'dfrn-confirm'))
104                 $errors ++;
105         if(! x($a,'dfrn-notify'))
106                 $errors ++;
107         if(! x($a,'dfrn-poll'))
108                 $errors ++;
109         return $errors;
110 }}
111
112 if(! function_exists('scrape_meta')) {
113 function scrape_meta($url) {
114
115         $a = get_app();
116
117         $ret = array();
118
119         logger('scrape_meta: url=' . $url);
120
121         $s = fetch_url($url);
122
123         if(! $s)
124                 return $ret;
125
126         $headers = $a->get_curl_headers();
127         logger('scrape_meta: headers=' . $headers, LOGGER_DEBUG);
128
129         $lines = explode("\n",$headers);
130         if(count($lines)) {
131                 foreach($lines as $line) {
132                         // don't try and run feeds through the html5 parser
133                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
134                                 return ret;
135                 }
136         }
137
138         try {
139                 $dom = HTML5_Parser::parse($s);
140         } catch (DOMException $e) {
141                 logger('scrape_meta: parse error: ' . $e);
142         }
143
144         if(! $dom)
145                 return $ret;
146
147         $items = $dom->getElementsByTagName('meta');
148
149         // get DFRN link elements
150
151         foreach($items as $item) {
152                 $x = $item->getAttribute('name');
153                 if(substr($x,0,5) == "dfrn-")
154                         $ret[$x] = $item->getAttribute('content');
155         }
156
157         return $ret;
158 }}
159
160
161 if(! function_exists('scrape_vcard')) {
162 function scrape_vcard($url) {
163
164         $a = get_app();
165
166         $ret = array();
167
168         logger('scrape_vcard: url=' . $url);
169
170         $s = fetch_url($url);
171
172         if(! $s)
173                 return $ret;
174
175         $headers = $a->get_curl_headers();
176         $lines = explode("\n",$headers);
177         if(count($lines)) {
178                 foreach($lines as $line) {
179                         // don't try and run feeds through the html5 parser
180                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
181                                 return ret;
182                 }
183         }
184
185         try {
186                 $dom = HTML5_Parser::parse($s);
187         } catch (DOMException $e) {
188                 logger('scrape_vcard: parse error: ' . $e);
189         }
190
191         if(! $dom)
192                 return $ret;
193
194         // Pull out hCard profile elements
195
196         $largest_photo = 0;
197
198         $items = $dom->getElementsByTagName('*');
199         foreach($items as $item) {
200                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
201                         $level2 = $item->getElementsByTagName('*');
202                         foreach($level2 as $x) {
203                                 if(attribute_contains($x->getAttribute('class'),'fn'))
204                                         $ret['fn'] = $x->textContent;
205                                 if((attribute_contains($x->getAttribute('class'),'photo'))
206                                         || (attribute_contains($x->getAttribute('class'),'avatar'))) {
207                                         $size = intval($x->getAttribute('width'));
208                                         if(($size > $largest_photo) || (! $largest_photo)) {
209                                                 $ret['photo'] = $x->getAttribute('src');
210                                                 $largest_photo = $size;
211                                         }
212                                 }
213                                 if((attribute_contains($x->getAttribute('class'),'nickname'))
214                                         || (attribute_contains($x->getAttribute('class'),'uid'))) {
215                                         $ret['nick'] = $x->textContent;
216                                 }
217                         }
218                 }
219         }
220
221         return $ret;
222 }}
223
224
225 if(! function_exists('scrape_feed')) {
226 function scrape_feed($url) {
227
228         $a = get_app();
229
230         $ret = array();
231         $s = fetch_url($url);
232
233         $headers = $a->get_curl_headers();
234         $code = $a->get_curl_code();
235
236         logger('scrape_feed: returns: ' . $code . ' headers=' . $headers, LOGGER_DEBUG);
237
238         if(! $s) {
239                 logger('scrape_feed: no data returned for ' . $url);
240                 return $ret;
241         }
242
243
244         $lines = explode("\n",$headers);
245         if(count($lines)) {
246                 foreach($lines as $line) {
247                         if(stristr($line,'content-type:')) {
248                                 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
249                                         $ret['feed_atom'] = $url;
250                                         return $ret;
251                                 }
252                                 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
253                                         $ret['feed_rss'] = $url;
254                                         return $ret;
255                                 }
256                         }
257                 }
258                 // perhaps an RSS version 1 feed with a generic or incorrect content-type?
259                 if(stristr($s,'</item>')) {
260                         $ret['feed_rss'] = $url;
261                         return $ret;
262                 }
263         }
264
265         try {
266                 $dom = HTML5_Parser::parse($s);
267         } catch (DOMException $e) {
268                 logger('scrape_feed: parse error: ' . $e);
269         }
270
271         if(! $dom) {
272                 logger('scrape_feed: failed to parse.');
273                 return $ret;
274         }
275
276
277         $head = $dom->getElementsByTagName('base');
278         if($head) {
279                 foreach($head as $head0) {
280                         $basename = $head0->getAttribute('href');
281                         break;
282                 }
283         }
284         if(! $basename)
285                 $basename = implode('/', array_slice(explode('/',$url),0,3)) . '/';
286
287         $items = $dom->getElementsByTagName('link');
288
289         // get Atom/RSS link elements, take the first one of either.
290
291         if($items) {
292                 foreach($items as $item) {
293                         $x = $item->getAttribute('rel');
294                         if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
295                                 if(! x($ret,'feed_atom'))
296                                         $ret['feed_atom'] = $item->getAttribute('href');
297                         }
298                         if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
299                                 if(! x($ret,'feed_rss'))
300                                         $ret['feed_rss'] = $item->getAttribute('href');
301                         }
302                 }
303         }
304
305         // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
306
307         if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
308                 $ret['feed_atom'] = $basename . $ret['feed_atom'];
309         if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
310                 $ret['feed_rss'] = $basename . $ret['feed_rss'];
311
312         return $ret;
313 }}
314
315
316 /**
317  *
318  * Probe a network address to discover what kind of protocols we need to communicate with it.
319  *
320  * Warning: this function is a bit touchy and there are some subtle dependencies within the logic flow.
321  * Edit with care.
322  *
323  */
324
325 /**
326  *
327  * PROBE_DIASPORA has a bias towards returning Diaspora information
328  * while PROBE_NORMAL has a bias towards dfrn/zot - in the case where
329  * an address (such as a Friendica address) supports more than one type
330  * of network. 
331  *
332  */
333
334
335 define ( 'PROBE_NORMAL',   0);
336 define ( 'PROBE_DIASPORA', 1);
337
338 function probe_url($url, $mode = PROBE_NORMAL) {
339         require_once('include/email.php');
340
341         $result = array();
342
343         if(! $url)
344                 return $result;
345
346         $result = Cache::get("probe_url:".$mode.":".$url);
347         if (!is_null($result)) {
348                 $result = unserialize($result);
349                 return $result;
350         }
351
352         $network = null;
353         $diaspora = false;
354         $diaspora_base = '';
355         $diaspora_guid = '';
356         $diaspora_key = '';
357         $has_lrdd = false;
358         $email_conversant = false;
359         $connectornetworks = false;
360         $appnet = false;
361
362         if (strpos($url,'twitter.com')) {
363                 $connectornetworks = true;
364                 $network = NETWORK_TWITTER;
365         }
366
367         if (strpos($url,'www.facebook.com')) {
368                 $connectornetworks = true;
369                 $network = NETWORK_FACEBOOK;
370         }
371
372         if (strpos($url,'alpha.app.net')) {
373                 $appnet = true;
374                 $network = NETWORK_APPNET;
375         }
376
377         // Twitter is deactivated since twitter closed its old API
378         //$twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
379         $lastfm  = ((strpos($url,'last.fm/user') !== false) ? true : false);
380
381         $at_addr = ((strpos($url,'@') !== false) ? true : false);
382
383         if((!$appnet) && (!$lastfm) && !$connectornetworks) {
384
385                 if(strpos($url,'mailto:') !== false && $at_addr) {
386                         $url = str_replace('mailto:','',$url);
387                         $links = array();
388                 }
389                 else
390                         $links = lrdd($url);
391
392                 if(count($links)) {
393                         $has_lrdd = true;
394
395                         logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
396                         foreach($links as $link) {
397                                 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
398                                         $zot = unamp($link['@attributes']['href']);
399                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
400                                         $dfrn = unamp($link['@attributes']['href']);
401                                 if($link['@attributes']['rel'] === 'salmon')
402                                         $notify = unamp($link['@attributes']['href']);
403                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
404                                         $poll = unamp($link['@attributes']['href']);
405                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
406                                         $hcard = unamp($link['@attributes']['href']);
407                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
408                                         $profile = unamp($link['@attributes']['href']);
409                                 if($link['@attributes']['rel'] === 'http://portablecontacts.net/spec/1.0')
410                                         $poco = unamp($link['@attributes']['href']);
411                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
412                                         $diaspora_base = unamp($link['@attributes']['href']);
413                                         $diaspora = true;
414                                 }
415                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
416                                         $diaspora_guid = unamp($link['@attributes']['href']);
417                                         $diaspora = true;
418                                 }
419                                 if($link['@attributes']['rel'] === 'diaspora-public-key') {
420                                         $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
421                                         if(strstr($diaspora_key,'RSA '))
422                                                 $pubkey = rsatopem($diaspora_key);
423                                         else
424                                                 $pubkey = $diaspora_key;
425                                         $diaspora = true;
426                                 }
427                                 if($link['@attributes']['rel'] === 'http://ostatus.org/schema/1.0/subscribe') {
428                                         $diaspora = false;
429                                 }
430                         }
431
432                         // Status.Net can have more than one profile URL. We need to match the profile URL
433                         // to a contact on incoming messages to prevent spam, and we won't know which one
434                         // to match. So in case of two, one of them is stored as an alias. Only store URL's
435                         // and not webfinger user@host aliases. If they've got more than two non-email style
436                         // aliases, let's hope we're lucky and get one that matches the feed author-uri because 
437                         // otherwise we're screwed.
438
439                         foreach($links as $link) {
440                                 if($link['@attributes']['rel'] === 'alias') {
441                                         if(strpos($link['@attributes']['href'],'@') === false) {
442                                                 if(isset($profile)) {
443                                                         if($link['@attributes']['href'] !== $profile)
444                                                                 $alias = unamp($link['@attributes']['href']);
445                                                 }
446                                                 else
447                                                         $profile = unamp($link['@attributes']['href']);
448                                         }
449                                 }
450                         }
451                 }
452                 elseif($mode == PROBE_NORMAL) {
453
454                         // Check email
455
456                         $orig_url = $url;
457                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
458                                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
459                                         intval(local_user())
460                                 );
461                                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
462                                         intval(local_user())
463                                 );
464                                 if(count($x) && count($r)) {
465                                         $mailbox = construct_mailbox_name($r[0]);
466                                         $password = '';
467                                         openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
468                                         $mbox = email_connect($mailbox,$r[0]['user'],$password);
469                                         if(! $mbox)
470                                                 logger('probe_url: email_connect failed.');
471                                         unset($password);
472                                 }
473                                 if($mbox) {
474                                         $msgs = email_poll($mbox,$orig_url);
475                                         logger('probe_url: searching ' . $orig_url . ', ' . count($msgs) . ' messages found.', LOGGER_DEBUG);
476                                         if(count($msgs)) {
477                                                 $addr = $orig_url;
478                                                 $network = NETWORK_MAIL;
479                                                 $name = substr($url,0,strpos($url,'@'));
480                                                 $phost = substr($url,strpos($url,'@')+1);
481                                                 $profile = 'http://' . $phost;
482                                                 // fix nick character range
483                                                 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => avatar_img($url));
484                                                 $notify = 'smtp ' . random_string();
485                                                 $poll = 'email ' . random_string();
486                                                 $priority = 0;
487                                                 $x = email_msg_meta($mbox,$msgs[0]);
488                                                 if(stristr($x[0]->from,$orig_url))
489                                                         $adr = imap_rfc822_parse_adrlist($x[0]->from,'');
490                                                 elseif(stristr($x[0]->to,$orig_url))
491                                                         $adr = imap_rfc822_parse_adrlist($x[0]->to,'');
492                                                 if(isset($adr)) {
493                                                         foreach($adr as $feadr) {
494                                                                 if((strcasecmp($feadr->mailbox,$name) == 0)
495                                                                         &&(strcasecmp($feadr->host,$phost) == 0)
496                                                                         && (strlen($feadr->personal))) {
497
498                                                                         $personal = imap_mime_header_decode($feadr->personal);
499                                                                         $vcard['fn'] = "";
500                                                                         foreach($personal as $perspart)
501                                                                                 if ($perspart->charset != "default")
502                                                                                         $vcard['fn'] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
503                                                                                 else
504                                                                                         $vcard['fn'] .= $perspart->text;
505
506                                                                         $vcard['fn'] = notags($vcard['fn']);
507                                                                 }
508                                                         }
509                                                 }
510                                         }
511                                         imap_close($mbox);
512                                 }
513                         }
514                 }
515         }
516
517         if($mode == PROBE_NORMAL) {
518                 if(strlen($zot)) {
519                         $s = fetch_url($zot);
520                         if($s) {
521                                 $j = json_decode($s);
522                                 if($j) {
523                                         $network = NETWORK_ZOT;
524                                         $vcard   = array(
525                                                 'fn'    => $j->fullname, 
526                                                 'nick'  => $j->nickname, 
527                                                 'photo' => $j->photo
528                                         );
529                                         $profile  = $j->url;
530                                         $notify   = $j->post;
531                                         $pubkey   = $j->pubkey;
532                                         $poll     = 'N/A';
533                                 }
534                         }
535                 }
536
537                 if(strlen($dfrn)) {
538                         $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
539                         if(is_array($ret) && x($ret,'dfrn-request')) {
540                                 $network = NETWORK_DFRN;
541                                 $request = $ret['dfrn-request'];
542                                 $confirm = $ret['dfrn-confirm'];
543                                 $notify  = $ret['dfrn-notify'];
544                                 $poll    = $ret['dfrn-poll'];
545
546                                 $vcard = array();
547                                 $vcard['fn'] = $ret['fn'];
548                                 $vcard['nick'] = $ret['nick'];
549                                 $vcard['photo'] = $ret['photo'];
550                         }
551                 }
552         }
553
554         if($diaspora && $diaspora_base && $diaspora_guid) {
555                 if($mode == PROBE_DIASPORA || ! $notify) {
556                         $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
557                         $batch  = $diaspora_base . 'receive/public' ;
558                 }
559                 if(strpos($url,'@'))
560                         $addr = str_replace('acct:', '', $url);
561         }
562
563         if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
564                 if($diaspora)
565                         $network = NETWORK_DIASPORA;
566                 elseif($has_lrdd)
567                         $network  = NETWORK_OSTATUS;
568                 $priority = 0;
569
570                 if($hcard && ! $vcard) {
571                         $vcard = scrape_vcard($hcard);
572
573                         // Google doesn't use absolute url in profile photos
574
575                         if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
576                                 $h = @parse_url($hcard);
577                                 if($h)
578                                         $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
579                         }
580
581                         logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
582                 }
583
584                 if($diaspora && $addr) {
585                         // Diaspora returns the name as the nick. As the nick will never be updated,
586                         // let's use the Diaspora nickname (the first part of the handle) as the nick instead
587                         $addr_parts = explode('@', $addr);
588                         $vcard['nick'] = $addr_parts[0];
589                 }
590
591                 /* if($twitter) {
592                         logger('twitter: setup');
593                         $tid = basename($url);
594                         $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
595                         if(intval($tid))
596                                 $poll = $tapi . '?user_id=' . $tid;
597                         else
598                                 $poll = $tapi . '?screen_name=' . $tid;
599                         $profile = 'http://twitter.com/#!/' . $tid;
600                         //$vcard['photo'] = 'https://api.twitter.com/1/users/profile_image/' . $tid;
601                         $vcard['photo'] = 'https://api.twitter.com/1/users/profile_image?screen_name=' . $tid . '&size=bigger';
602                         $vcard['nick'] = $tid;
603                         $vcard['fn'] = $tid;
604                 } */
605
606                 if($lastfm) {
607                         $profile = $url;
608                         $poll = str_replace(array('www.','last.fm/'),array('','ws.audioscrobbler.com/1.0/'),$url) . '/recenttracks.rss';
609                         $vcard['nick'] = basename($url);
610                         $vcard['fn'] = $vcard['nick'] . t(' on Last.fm');
611                         $network = NETWORK_FEED;
612                 }
613
614                 if(! x($vcard,'fn'))
615                         if(x($vcard,'nick'))
616                                 $vcard['fn'] = $vcard['nick'];
617
618                 $check_feed = false;
619
620                 if(stristr($url,'tumblr.com') && (! stristr($url,'/rss'))) {
621                         $poll = $url . '/rss';
622                         $check_feed = true;
623                         // Will leave it to others to figure out how to grab the avatar, which is on the $url page in the open graph meta links
624                 }
625
626                 if($appnet || ! $poll)
627                         $check_feed = true;
628                 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
629                         $check_feed = true;
630                 if(($at_addr) && (! count($links)))
631                         $check_feed = false;
632
633                 if ($connectornetworks)
634                         $check_feed = false;
635
636                 if($check_feed) {
637
638                         $feedret = scrape_feed(($poll) ? $poll : $url);
639                         logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
640                         if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
641                                 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
642                                 if(! x($vcard))
643                                         $vcard = array();
644                         }
645
646                         if(x($feedret,'photo') && (! x($vcard,'photo')))
647                                 $vcard['photo'] = $feedret['photo'];
648                         require_once('library/simplepie/simplepie.inc');
649                         $feed = new SimplePie();
650                         $xml = fetch_url($poll);
651
652                         logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
653                         $a = get_app();
654
655                         logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), LOGGER_DATA);
656
657                         // Don't try and parse an empty string
658                         $feed->set_raw_data(($xml) ? $xml : '<?xml version="1.0" encoding="utf-8" ?><xml></xml>');
659
660                         $feed->init();
661                         if($feed->error())
662                                 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
663
664
665                         if(! x($vcard,'photo'))
666                                 $vcard['photo'] = $feed->get_image_url();
667                         $author = $feed->get_author();
668
669                         if($author) {
670                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
671                                 if(! $vcard['fn'])
672                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
673                                 if(strpos($vcard['fn'],'@') !== false)
674                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
675                                 $email = unxmlify($author->get_email());
676                                 if(! $profile && $author->get_link())
677                                         $profile = trim(unxmlify($author->get_link()));
678                                 if(! $vcard['photo']) {
679                                         $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
680                                         if($rawtags) {
681                                                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
682                                                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
683                                                         $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
684                                         }
685                                 }
686                         }
687                         else {
688                                 $item = $feed->get_item(0);
689                                 if($item) {
690                                         $author = $item->get_author();
691                                         if($author) {
692                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
693                                                 if(! $vcard['fn'])
694                                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
695                                                 if(strpos($vcard['fn'],'@') !== false)
696                                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
697                                                 $email = unxmlify($author->get_email());
698                                                 if(! $profile && $author->get_link())
699                                                         $profile = trim(unxmlify($author->get_link()));
700                                         }
701                                         if(! $vcard['photo']) {
702                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
703                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
704                                                         $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
705                                         }
706                                         if(! $vcard['photo']) {
707                                                 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
708                                                 if($rawtags) {
709                                                         $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
710                                                         if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
711                                                                 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
712                                                 }
713                                         }
714                                 }
715                         }
716
717                         if((! $vcard['photo']) && strlen($email))
718                                 $vcard['photo'] = avatar_img($email);
719                         if($poll === $profile)
720                                 $lnk = $feed->get_permalink();
721                         if(isset($lnk) && strlen($lnk))
722                                 $profile = $lnk;
723
724                         if(! $network) {
725                                 $network = NETWORK_FEED;
726                                 // If it is a feed, don't take the author name as feed name
727                                 unset($vcard['fn']);
728                         }
729                         if(! (x($vcard,'fn')))
730                                 $vcard['fn'] = notags($feed->get_title());
731                         if(! (x($vcard,'fn')))
732                                 $vcard['fn'] = notags($feed->get_description());
733
734                         if(strpos($vcard['fn'],'Twitter / ') !== false) {
735                                 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
736                                 $vcard['fn'] = trim($vcard['fn']);
737                         }
738                         if(! x($vcard,'nick')) {
739                                 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
740                                 if(strpos($vcard['nick'],' '))
741                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
742                         }
743                         if(! $priority)
744                                 $priority = 2;
745                 }
746         }
747
748         if(! x($vcard,'photo')) {
749                 $a = get_app();
750                 $vcard['photo'] = $a->get_baseurl() . '/images/person-175.jpg' ;
751         }
752
753         if(! $profile)
754                 $profile = $url;
755
756         // No human could be associated with this link, use the URL as the contact name
757
758         if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
759                 $vcard['fn'] = $url;
760
761         $vcard['fn'] = notags($vcard['fn']);
762         $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
763
764         $result['name'] = $vcard['fn'];
765         $result['nick'] = $vcard['nick'];
766         $result['url'] = $profile;
767         $result['addr'] = $addr;
768         $result['batch'] = $batch;
769         $result['notify'] = $notify;
770         $result['poll'] = $poll;
771         $result['request'] = $request;
772         $result['confirm'] = $confirm;
773         $result['poco'] = $poco;
774         $result['photo'] = $vcard['photo'];
775         $result['priority'] = $priority;
776         $result['network'] = $network;
777         $result['alias'] = $alias;
778         $result['pubkey'] = $pubkey;
779
780         logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
781
782         // Trying if it maybe a diaspora account
783         if ($result['network'] == NETWORK_FEED) {
784                 require_once('include/bbcode.php');
785                 $address = GetProfileUsername($url, "", true);
786                 $result2 = probe_url($address, $mode);
787                 if ($result2['network'] != "")
788                         $result = $result2;
789         }
790
791         Cache::set("probe_url:".$mode.":".$url,serialize($result));
792
793         return $result;
794 }