]> git.mxchange.org Git - friendica.git/blob - include/Scrape.php
don't try to parse empty xml with simplepie, throws conversion warning
[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         $network = null;
347         $diaspora = false;
348         $diaspora_base = '';
349         $diaspora_guid = '';
350         $diaspora_key = '';
351         $has_lrdd = false;
352         $email_conversant = false;
353
354         $twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
355         $lastfm  = ((strpos($url,'last.fm/user') !== false) ? true : false);
356
357         $at_addr = ((strpos($url,'@') !== false) ? true : false);
358
359         if((! $twitter) && (! $lastfm)) {
360
361                 if(strpos($url,'mailto:') !== false && $at_addr) {
362                         $url = str_replace('mailto:','',$url);
363                         $links = array();
364                 }
365                 else
366                         $links = lrdd($url);
367
368                 if(count($links)) {
369                         $has_lrdd = true;
370
371                         logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
372                         foreach($links as $link) {
373                                 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
374                                         $zot = unamp($link['@attributes']['href']);
375                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
376                                         $dfrn = unamp($link['@attributes']['href']);
377                                 if($link['@attributes']['rel'] === 'salmon')
378                                         $notify = unamp($link['@attributes']['href']);
379                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
380                                         $poll = unamp($link['@attributes']['href']);
381                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
382                                         $hcard = unamp($link['@attributes']['href']);
383                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
384                                         $profile = unamp($link['@attributes']['href']);
385                                 if($link['@attributes']['rel'] === 'http://portablecontacts.net/spec/1.0')
386                                         $poco = unamp($link['@attributes']['href']);
387                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
388                                         $diaspora_base = unamp($link['@attributes']['href']);
389                                         $diaspora = true;
390                                 }
391                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
392                                         $diaspora_guid = unamp($link['@attributes']['href']);
393                                         $diaspora = true;
394                                 }
395                                 if($link['@attributes']['rel'] === 'diaspora-public-key') {
396                                         $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
397                                         if(strstr($diaspora_key,'RSA '))
398                                                 $pubkey = rsatopem($diaspora_key);
399                                         else
400                                                 $pubkey = $diaspora_key;
401                                         $diaspora = true;
402                                 }
403                         }
404
405                         // Status.Net can have more than one profile URL. We need to match the profile URL
406                         // to a contact on incoming messages to prevent spam, and we won't know which one
407                         // to match. So in case of two, one of them is stored as an alias. Only store URL's
408                         // and not webfinger user@host aliases. If they've got more than two non-email style
409                         // aliases, let's hope we're lucky and get one that matches the feed author-uri because 
410                         // otherwise we're screwed.
411
412                         foreach($links as $link) {
413                                 if($link['@attributes']['rel'] === 'alias') {
414                                         if(strpos($link['@attributes']['href'],'@') === false) {
415                                                 if(isset($profile)) {
416                                                         if($link['@attributes']['href'] !== $profile)
417                                                                 $alias = unamp($link['@attributes']['href']);
418                                                 }
419                                                 else
420                                                         $profile = unamp($link['@attributes']['href']);
421                                         }
422                                 }
423                         }
424                 }
425                 elseif($mode == PROBE_NORMAL) {
426
427                         // Check email
428
429                         $orig_url = $url;
430                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
431                                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
432                                         intval(local_user())
433                                 );
434                                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
435                                         intval(local_user())
436                                 );
437                                 if(count($x) && count($r)) {
438                                         $mailbox = construct_mailbox_name($r[0]);
439                                         $password = '';
440                                         openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
441                                         $mbox = email_connect($mailbox,$r[0]['user'],$password);
442                                         if(! $mbox)
443                                                 logger('probe_url: email_connect failed.');
444                                         unset($password);
445                                 }
446                                 if($mbox) {
447                                         $msgs = email_poll($mbox,$orig_url);
448                                         logger('probe_url: searching ' . $orig_url . ', ' . count($msgs) . ' messages found.', LOGGER_DEBUG);
449                                         if(count($msgs)) {
450                                                 $addr = $orig_url;
451                                                 $network = NETWORK_MAIL;
452                                                 $name = substr($url,0,strpos($url,'@'));
453                                                 $phost = substr($url,strpos($url,'@')+1);
454                                                 $profile = 'http://' . $phost;
455                                                 // fix nick character range
456                                                 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => avatar_img($url));
457                                                 $notify = 'smtp ' . random_string();
458                                                 $poll = 'email ' . random_string();
459                                                 $priority = 0;
460                                                 $x = email_msg_meta($mbox,$msgs[0]);
461                                                 if(stristr($x[0]->from,$orig_url))
462                                                         $adr = imap_rfc822_parse_adrlist($x[0]->from,'');
463                                                 elseif(stristr($x[0]->to,$orig_url))
464                                                         $adr = imap_rfc822_parse_adrlist($x[0]->to,'');
465                                                 if(isset($adr)) {
466                                                         foreach($adr as $feadr) {
467                                                                 if((strcasecmp($feadr->mailbox,$name) == 0)
468                                                                         &&(strcasecmp($feadr->host,$phost) == 0)
469                                                                         && (strlen($feadr->personal))) {
470
471                                                                         $personal = imap_mime_header_decode($feadr->personal);
472                                                                         $vcard['fn'] = "";
473                                                                         foreach($personal as $perspart)
474                                                                                 if ($perspart->charset != "default")
475                                                                                         $vcard['fn'] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
476                                                                                 else
477                                                                                         $vcard['fn'] .= $perspart->text;
478
479                                                                         $vcard['fn'] = notags($vcard['fn']);
480                                                                 }
481                                                         }
482                                                 }
483                                         }
484                                         imap_close($mbox);
485                                 }
486                         }
487                 }
488         }
489
490         if($mode == PROBE_NORMAL) {
491                 if(strlen($zot)) {
492                         $s = fetch_url($zot);
493                         if($s) {
494                                 $j = json_decode($s);
495                                 if($j) {
496                                         $network = NETWORK_ZOT;
497                                         $vcard   = array(
498                                                 'fn'    => $j->fullname, 
499                                                 'nick'  => $j->nickname, 
500                                                 'photo' => $j->photo
501                                         );
502                                         $profile  = $j->url;
503                                         $notify   = $j->post;
504                                         $pubkey   = $j->pubkey;
505                                         $poll     = 'N/A';
506                                 }
507                         }
508                 }
509
510                 if(strlen($dfrn)) {
511                         $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
512                         if(is_array($ret) && x($ret,'dfrn-request')) {
513                                 $network = NETWORK_DFRN;
514                                 $request = $ret['dfrn-request'];
515                                 $confirm = $ret['dfrn-confirm'];
516                                 $notify  = $ret['dfrn-notify'];
517                                 $poll    = $ret['dfrn-poll'];
518
519                                 $vcard = array();
520                                 $vcard['fn'] = $ret['fn'];
521                                 $vcard['nick'] = $ret['nick'];
522                                 $vcard['photo'] = $ret['photo'];
523                         }
524                 }
525         }
526
527         if($diaspora && $diaspora_base && $diaspora_guid) {
528                 if($mode == PROBE_DIASPORA || ! $notify) {
529                         $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
530                         $batch  = $diaspora_base . 'receive/public' ;
531                 }
532                 if(strpos($url,'@'))
533                         $addr = str_replace('acct:', '', $url);
534         }
535
536         if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
537                 if($diaspora)
538                         $network = NETWORK_DIASPORA;
539                 elseif($has_lrdd)
540                         $network  = NETWORK_OSTATUS;
541                 $priority = 0;
542
543                 if($hcard && ! $vcard) {
544                         $vcard = scrape_vcard($hcard);
545
546                         // Google doesn't use absolute url in profile photos
547
548                         if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
549                                 $h = @parse_url($hcard);
550                                 if($h)
551                                         $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
552                         }
553
554                         logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
555                 }
556
557                 if($diaspora && $addr) {
558                         // Diaspora returns the name as the nick. As the nick will never be updated,
559                         // let's use the Diaspora nickname (the first part of the handle) as the nick instead
560                         $addr_parts = explode('@', $addr);
561                         $vcard['nick'] = $addr_parts[0];
562                 }
563
564                 if($twitter) {
565                         logger('twitter: setup');
566                         $tid = basename($url);
567                         $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
568                         if(intval($tid))
569                                 $poll = $tapi . '?user_id=' . $tid;
570                         else
571                                 $poll = $tapi . '?screen_name=' . $tid;
572                         $profile = 'http://twitter.com/#!/' . $tid;
573                         //$vcard['photo'] = 'https://api.twitter.com/1/users/profile_image/' . $tid;
574                         $vcard['photo'] = 'https://api.twitter.com/1/users/profile_image?screen_name=' . $tid . '&size=bigger';
575                         $vcard['nick'] = $tid;
576                         $vcard['fn'] = $tid;
577                 }
578
579                 if($lastfm) {
580                         $profile = $url;
581                         $poll = str_replace(array('www.','last.fm/'),array('','ws.audioscrobbler.com/1.0/'),$url) . '/recenttracks.rss';
582                         $vcard['nick'] = basename($url);
583                         $vcard['fn'] = $vcard['nick'] . t(' on Last.fm');
584                         $network = NETWORK_FEED;
585                 }
586
587                 if(! x($vcard,'fn'))
588                         if(x($vcard,'nick'))
589                                 $vcard['fn'] = $vcard['nick'];
590
591                 $check_feed = false;
592
593                 if(stristr($url,'tumblr.com') && (! stristr($url,'/rss'))) {
594                         $poll = $url . '/rss';
595                         $check_feed = true;
596                         // 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
597                 }
598
599                 if($twitter || ! $poll)
600                         $check_feed = true;
601                 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
602                         $check_feed = true;
603                 if(($at_addr) && (! count($links)))
604                         $check_feed = false;
605
606                 if($check_feed) {
607
608                         $feedret = scrape_feed(($poll) ? $poll : $url);
609                         logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
610                         if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
611                                 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
612                                 if(! x($vcard)) 
613                                         $vcard = array();
614                         }
615
616                         if(x($feedret,'photo') && (! x($vcard,'photo')))
617                                 $vcard['photo'] = $feedret['photo'];
618                         require_once('library/simplepie/simplepie.inc');
619                     $feed = new SimplePie();
620                         $xml = fetch_url($poll);
621
622                         logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
623                         $a = get_app();
624
625                         logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), LOGGER_DATA);
626
627                         // Don't try and parse an empty string
628                         $feed->set_raw_data(($xml) ? $xml : '<?xml version="1.0" encoding="utf-8" ?><xml></xml>');
629
630                     $feed->init();
631                         if($feed->error())
632                                 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
633
634
635                         if(! x($vcard,'photo'))
636                                 $vcard['photo'] = $feed->get_image_url();
637                         $author = $feed->get_author();
638
639                         if($author) {                   
640                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
641                                 if(! $vcard['fn'])
642                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
643                                 if(strpos($vcard['fn'],'@') !== false)
644                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
645                                 $email = unxmlify($author->get_email());
646                                 if(! $profile && $author->get_link())
647                                         $profile = trim(unxmlify($author->get_link()));
648                                 if(! $vcard['photo']) {
649                                         $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
650                                 if($rawtags) {
651                                                 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
652                                                 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
653                                                         $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
654                                 }
655                                 }
656                         }
657                         else {
658                                 $item = $feed->get_item(0);
659                                 if($item) {
660                                         $author = $item->get_author();
661                                         if($author) {                   
662                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
663                                                 if(! $vcard['fn'])
664                                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
665                                                 if(strpos($vcard['fn'],'@') !== false)
666                                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
667                                                 $email = unxmlify($author->get_email());
668                                                 if(! $profile && $author->get_link())
669                                                         $profile = trim(unxmlify($author->get_link()));
670                                         }
671                                         if(! $vcard['photo']) {
672                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
673                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
674                                                         $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
675                                         }
676                                         if(! $vcard['photo']) {
677                                                 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
678                                         if($rawtags) {
679                                                         $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
680                                                         if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
681                                                                 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
682                                         }
683                                         }
684                                 }
685                         }
686
687                         if((! $vcard['photo']) && strlen($email))
688                                 $vcard['photo'] = avatar_img($email);
689                         if($poll === $profile)
690                                 $lnk = $feed->get_permalink();
691                         if(isset($lnk) && strlen($lnk))
692                                 $profile = $lnk;        
693
694                         if(! (x($vcard,'fn')))
695                                 $vcard['fn'] = notags($feed->get_title());
696                         if(! (x($vcard,'fn')))
697                                 $vcard['fn'] = notags($feed->get_description());
698
699                         if(strpos($vcard['fn'],'Twitter / ') !== false) {
700                                 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
701                                 $vcard['fn'] = trim($vcard['fn']);
702                         }
703                         if(! x($vcard,'nick')) {
704                                 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
705                                 if(strpos($vcard['nick'],' '))
706                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
707                         }
708                         if(! $network)
709                                 $network = NETWORK_FEED;
710                         if(! $priority)
711                                 $priority = 2;
712                 }
713         }
714
715         if(! x($vcard,'photo')) {
716                 $a = get_app();
717                 $vcard['photo'] = $a->get_baseurl() . '/images/person-175.jpg' ; 
718         }
719
720         if(! $profile)
721                 $profile = $url;
722
723         // No human could be associated with this link, use the URL as the contact name
724
725         if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
726                 $vcard['fn'] = $url;
727
728         $vcard['fn'] = notags($vcard['fn']);
729         $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
730                 
731         $result['name'] = $vcard['fn'];
732         $result['nick'] = $vcard['nick'];
733         $result['url'] = $profile;
734         $result['addr'] = $addr;
735         $result['batch'] = $batch;
736         $result['notify'] = $notify;
737         $result['poll'] = $poll;
738         $result['request'] = $request;
739         $result['confirm'] = $confirm;
740         $result['poco'] = $poco;
741         $result['photo'] = $vcard['photo'];
742         $result['priority'] = $priority;
743         $result['network'] = $network;
744         $result['alias'] = $alias;
745         $result['pubkey'] = $pubkey;
746
747         logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
748
749         return $result;
750 }