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