]> git.mxchange.org Git - friendica.git/blob - include/Scrape.php
added test blueprints, fixed? encoding issues
[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         }
259
260         try {
261                 $dom = HTML5_Parser::parse($s);
262         } catch (DOMException $e) {
263                 logger('scrape_feed: parse error: ' . $e);
264         }
265
266         if(! $dom) {
267                 logger('scrape_feed: failed to parse.');
268                 return $ret;
269         }
270
271
272         $head = $dom->getElementsByTagName('base');
273         if($head) {
274                 foreach($head as $head0) {
275                         $basename = $head0->getAttribute('href');
276                         break;
277                 }
278         }
279         if(! $basename)
280                 $basename = substr($url,0,strrpos($url,'/')) . '/';
281
282         $items = $dom->getElementsByTagName('link');
283
284         // get Atom/RSS link elements, take the first one of either.
285
286         if($items) {
287                 foreach($items as $item) {
288                         $x = $item->getAttribute('rel');
289                         if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
290                                 if(! x($ret,'feed_atom'))
291                                         $ret['feed_atom'] = $item->getAttribute('href');
292                         }
293                         if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
294                                 if(! x($ret,'feed_rss'))
295                                         $ret['feed_rss'] = $item->getAttribute('href');
296                         }
297                 }       
298         }
299
300         // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
301
302         if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
303                 $ret['feed_atom'] = $basename . $ret['feed_atom'];
304         if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
305                 $ret['feed_rss'] = $basename . $ret['feed_rss'];
306
307         return $ret;
308 }}
309
310
311 /**
312  *
313  * Probe a network address to discover what kind of protocols we need to communicate with it.
314  *
315  * Warning: this function is a bit touchy and there are some subtle dependencies within the logic flow.
316  * Edit with care.
317  *
318  */
319
320 /**
321  *
322  * PROBE_DIASPORA has a bias towards returning Diaspora information
323  * while PROBE_NORMAL has a bias towards dfrn/zot - in the case where
324  * an address (such as a Friendica address) supports more than one type
325  * of network. 
326  *
327  */
328
329
330 define ( 'PROBE_NORMAL',   0);
331 define ( 'PROBE_DIASPORA', 1);
332
333 function probe_url($url, $mode = PROBE_NORMAL) {
334         require_once('include/email.php');
335
336         $result = array();
337
338         if(! $url)
339                 return $result;
340
341         $network = null;
342         $diaspora = false;
343         $diaspora_base = '';
344         $diaspora_guid = '';    
345         $diaspora_key = '';
346         $has_lrdd = false;
347         $email_conversant = false;
348
349         $twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
350
351         $at_addr = ((strpos($url,'@') !== false) ? true : false);
352
353         if(! $twitter) {
354
355                 if(strpos($url,'mailto:') !== false && $at_addr) {
356                         $url = str_replace('mailto:','',$url);
357                         $links = array();
358                 }
359                 else
360                         $links = lrdd($url);
361
362                 if(count($links)) {
363                         $has_lrdd = true;
364
365                         logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
366                         foreach($links as $link) {
367                                 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
368                                         $zot = unamp($link['@attributes']['href']);
369                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
370                                         $dfrn = unamp($link['@attributes']['href']);
371                                 if($link['@attributes']['rel'] === 'salmon')
372                                         $notify = unamp($link['@attributes']['href']);
373                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
374                                         $poll = unamp($link['@attributes']['href']);
375                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
376                                         $hcard = unamp($link['@attributes']['href']);
377                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
378                                         $profile = unamp($link['@attributes']['href']);
379                                 if($link['@attributes']['rel'] === 'http://portablecontacts.net/spec/1.0')
380                                         $poco = unamp($link['@attributes']['href']);
381                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
382                                         $diaspora_base = unamp($link['@attributes']['href']);
383                                         $diaspora = true;
384                                 }
385                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
386                                         $diaspora_guid = unamp($link['@attributes']['href']);
387                                         $diaspora = true;
388                                 }
389                                 if($link['@attributes']['rel'] === 'diaspora-public-key') {
390                                         $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
391                                         $pubkey = rsatopem($diaspora_key);
392                                         $diaspora = true;
393                                 }
394                         }
395
396                         // Status.Net can have more than one profile URL. We need to match the profile URL
397                         // to a contact on incoming messages to prevent spam, and we won't know which one
398                         // to match. So in case of two, one of them is stored as an alias. Only store URL's
399                         // and not webfinger user@host aliases. If they've got more than two non-email style
400                         // aliases, let's hope we're lucky and get one that matches the feed author-uri because 
401                         // otherwise we're screwed.
402
403                         foreach($links as $link) {
404                                 if($link['@attributes']['rel'] === 'alias') {
405                                         if(strpos($link['@attributes']['href'],'@') === false) {
406                                                 if(isset($profile)) {
407                                                         if($link['@attributes']['href'] !== $profile)
408                                                                 $alias = unamp($link['@attributes']['href']);
409                                                 }
410                                                 else
411                                                         $profile = unamp($link['@attributes']['href']);
412                                         }
413                                 }
414                         }
415                 }
416                 elseif($mode == PROBE_NORMAL) {
417
418                         // Check email
419
420                         $orig_url = $url;
421                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
422                                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
423                                         intval(local_user())
424                                 );
425                                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
426                                         intval(local_user())
427                                 );
428                                 if(count($x) && count($r)) {
429                                     $mailbox = construct_mailbox_name($r[0]);
430                                         $password = '';
431                                         openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
432                                         $mbox = email_connect($mailbox,$r[0]['user'],$password);
433                                         unset($password);
434                                 }
435                                 if($mbox) {
436                                         $msgs = email_poll($mbox,$orig_url);
437                                         if(count($msgs)) {
438                                                 $addr = $orig_url;
439                                                 $network = NETWORK_MAIL;
440                                                 $name = substr($url,0,strpos($url,'@'));
441                                                 $phost = substr($url,strpos($url,'@')+1);
442                                                 $profile = 'http://' . $phost;
443                                                 // fix nick character range
444                                                 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
445                                                 $notify = 'smtp ' . random_string();
446                                                 $poll = 'email ' . random_string();
447                                                 $priority = 0;
448                                                 $x = email_msg_meta($mbox,$msgs[0]);
449                                                 if(stristr($x->from,$orig_url))
450                                                         $adr = imap_rfc822_parse_adrlist($x->from,'');
451                                                 elseif(stristr($x->to,$orig_url))
452                                                         $adr = imap_rfc822_parse_adrlist($x->to,'');
453                                                 if(isset($adr)) {
454                                                         foreach($adr as $feadr) {
455                                                                 if((strcasecmp($feadr->mailbox,$name) == 0)
456                                                                         &&(strcasecmp($feadr->host,$phost) == 0)
457                                                                         && (strlen($feadr->personal))) {
458
459                                                                         $personal = imap_mime_header_decode($feadr->personal);
460                                                                         $vcard['fn'] = "";
461                                                                         foreach($personal as $perspart)
462                                                                                 if ($perspart->charset != "default")
463                                                                                         $vcard['fn'] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
464                                                                                 else
465                                                                                         $vcard['fn'] .= $perspart->text;
466
467                                                                         $vcard['fn'] = notags($vcard['fn']);
468                                                                 }
469                                                         }
470                                                 }
471                                         }
472                                         imap_close($mbox);
473                                 }
474                         }
475                 }
476         }       
477
478         if($mode == PROBE_NORMAL) {
479                 if(strlen($zot)) {
480                         $s = fetch_url($zot);
481                         if($s) {
482                                 $j = json_decode($s);
483                                 if($j) {
484                                         $network = NETWORK_ZOT;
485                                         $vcard   = array(
486                                                 'fn'    => $j->fullname, 
487                                                 'nick'  => $j->nickname, 
488                                                 'photo' => $j->photo
489                                         );
490                                         $profile  = $j->url;
491                                         $notify   = $j->post;
492                                         $pubkey   = $j->pubkey;
493                                         $poll     = 'N/A';
494                                 }
495                         }
496                 }
497
498                 if(strlen($dfrn)) {
499                         $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
500                         if(is_array($ret) && x($ret,'dfrn-request')) {
501                                 $network = NETWORK_DFRN;
502                                 $request = $ret['dfrn-request'];
503                                 $confirm = $ret['dfrn-confirm'];
504                                 $notify  = $ret['dfrn-notify'];
505                                 $poll    = $ret['dfrn-poll'];
506
507                                 $vcard = array();
508                                 $vcard['fn'] = $ret['fn'];
509                                 $vcard['nick'] = $ret['nick'];
510                                 $vcard['photo'] = $ret['photo'];
511                         }
512                 }
513         }
514
515         if($diaspora && $diaspora_base && $diaspora_guid) {
516                 if($mode == PROBE_DIASPORA || ! $notify) {
517                         $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
518                         $batch  = $diaspora_base . 'receive/public' ;
519                 }
520                 if(strpos($url,'@'))
521                         $addr = str_replace('acct:', '', $url);
522         }                       
523
524         if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
525                 if($diaspora)
526                         $network = NETWORK_DIASPORA;
527                 elseif($has_lrdd)
528                         $network  = NETWORK_OSTATUS;
529                 $priority = 0;
530
531                 if($hcard && ! $vcard) {
532                         $vcard = scrape_vcard($hcard);
533
534                         // Google doesn't use absolute url in profile photos
535         
536                         if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
537                                 $h = @parse_url($hcard);
538                                 if($h)
539                                         $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
540                         }
541                 
542                         logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
543                 }
544
545                 if($twitter) {          
546                         logger('twitter: setup');
547                         $tid = basename($url);
548                         $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
549                         if(intval($tid))
550                                 $poll = $tapi . '?user_id=' . $tid;
551                         else
552                                 $poll = $tapi . '?screen_name=' . $tid;
553                         $profile = 'http://twitter.com/#!/' . $tid;
554                         $vcard['photo'] = 'https://api.twitter.com/1/users/profile_image/' . $tid;
555                         $vcard['nick'] = $tid;
556                         $vcard['fn'] = $tid . '@twitter';
557                 }
558
559                 if(! x($vcard,'fn'))
560                         if(x($vcard,'nick'))
561                                 $vcard['fn'] = $vcard['nick'];
562
563                 $check_feed = false;
564
565                 if($twitter || ! $poll)
566                         $check_feed = true;
567                 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
568                         $check_feed = true;
569                 if(($at_addr) && (! count($links)))
570                         $check_feed = false;
571
572                 if($check_feed) {
573
574                         $feedret = scrape_feed(($poll) ? $poll : $url);
575                         logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
576                         if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
577                                 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
578                                 if(! x($vcard)) 
579                                         $vcard = array();
580                         }
581
582                         if(x($feedret,'photo') && (! x($vcard,'photo')))
583                                 $vcard['photo'] = $feedret['photo'];
584                         require_once('library/simplepie/simplepie.inc');
585                     $feed = new SimplePie();
586                         $xml = fetch_url($poll);
587
588                         logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
589                         $a = get_app();
590
591                         logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), $LOGGER_DATA);
592
593                         $feed->set_raw_data($xml);
594
595                     $feed->init();
596                         if($feed->error())
597                                 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
598
599
600                         if(! x($vcard,'photo'))
601                                 $vcard['photo'] = $feed->get_image_url();
602                         $author = $feed->get_author();
603
604                         if($author) {                   
605                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
606                                 if(! $vcard['fn'])
607                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
608                                 if(strpos($vcard['fn'],'@') !== false)
609                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
610                                 $email = unxmlify($author->get_email());
611                                 if(! $profile && $author->get_link())
612                                         $profile = trim(unxmlify($author->get_link()));
613                                 if(! $vcard['photo']) {
614                                         $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
615                                 if($rawtags) {
616                                                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
617                                                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
618                                                         $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
619                                 }
620                                 }
621                         }
622                         else {
623                                 $item = $feed->get_item(0);
624                                 if($item) {
625                                         $author = $item->get_author();
626                                         if($author) {                   
627                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
628                                                 if(! $vcard['fn'])
629                                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
630                                                 if(strpos($vcard['fn'],'@') !== false)
631                                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
632                                                 $email = unxmlify($author->get_email());
633                                                 if(! $profile && $author->get_link())
634                                                         $profile = trim(unxmlify($author->get_link()));
635                                         }
636                                         if(! $vcard['photo']) {
637                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
638                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
639                                                         $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
640                                         }
641                                         if(! $vcard['photo']) {
642                                                 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
643                                         if($rawtags) {
644                                                         $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
645                                                         if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
646                                                                 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
647                                         }
648                                         }
649                                 }
650                         }
651
652                         if((! $vcard['photo']) && strlen($email))
653                                 $vcard['photo'] = gravatar_img($email);
654                         if($poll === $profile)
655                                 $lnk = $feed->get_permalink();
656                         if(isset($lnk) && strlen($lnk))
657                                 $profile = $lnk;        
658
659                         if(! (x($vcard,'fn')))
660                                 $vcard['fn'] = notags($feed->get_title());
661                         if(! (x($vcard,'fn')))
662                                 $vcard['fn'] = notags($feed->get_description());
663
664                         if(strpos($vcard['fn'],'Twitter / ') !== false) {
665                                 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
666                                 $vcard['fn'] = trim($vcard['fn']);
667                         }
668                         if(! x($vcard,'nick')) {
669                                 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
670                                 if(strpos($vcard['nick'],' '))
671                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
672                         }
673                         if(! $network)
674                                 $network = NETWORK_FEED;
675                         if(! $priority)
676                                 $priority = 2;
677                 }
678         }
679
680         if(! x($vcard,'photo')) {
681                 $a = get_app();
682                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
683         }
684
685         if(! $profile)
686                 $profile = $url;
687
688         // No human could be associated with this link, use the URL as the contact name
689
690         if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
691                 $vcard['fn'] = $url;
692
693         $vcard['fn'] = notags($vcard['fn']);
694         $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
695                 
696         $result['name'] = $vcard['fn'];
697         $result['nick'] = $vcard['nick'];
698         $result['url'] = $profile;
699         $result['addr'] = $addr;
700         $result['batch'] = $batch;
701         $result['notify'] = $notify;
702         $result['poll'] = $poll;
703         $result['request'] = $request;
704         $result['confirm'] = $confirm;
705         $result['poco'] = $poco;
706         $result['photo'] = $vcard['photo'];
707         $result['priority'] = $priority;
708         $result['network'] = $network;
709         $result['alias'] = $alias;
710         $result['pubkey'] = $pubkey;
711
712         logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
713
714         return $result;
715 }