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