]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
f30ecdc92132f71f14366b5389dc3839d78ad536
[friendica.git] / mod / follow.php
1 <?php
2
3 require_once('Scrape.php');
4
5 function follow_post(&$a) {
6
7         if(! local_user()) {
8                 notice( t('Permission denied.') . EOL);
9                 goaway($_SESSION['return_url']);
10                 // NOTREACHED
11         }
12
13         $url = $orig_url = notags(trim($_POST['url']));
14         $diaspora = false;      
15         $email_conversant = false;
16
17         if($url) {
18                 $links = lrdd($url);
19
20                 if(count($links)) {
21                         foreach($links as $link) {
22                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
23                                         $dfrn = unamp($link['@attributes']['href']);
24                                 if($link['@attributes']['rel'] === 'salmon')
25                                         $notify = unamp($link['@attributes']['href']);
26                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
27                                         $poll = unamp($link['@attributes']['href']);
28                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
29                                         $hcard = unamp($link['@attributes']['href']);
30                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
31                                         $profile = unamp($link['@attributes']['href']);
32                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location')
33                                         $diaspora = true;
34
35
36                         }
37
38                         // Status.Net can have more than one profile URL. We need to match the profile URL
39                         // to a contact on incoming messages to prevent spam, and we won't know which one
40                         // to match. So in case of two, one of them is stored as an alias. Only store URL's
41                         // and not webfinger user@host aliases. If they've got more than two non-email style
42                         // aliases, let's hope we're lucky and get one that matches the feed author-uri because 
43                         // otherwise we're screwed.
44
45                         foreach($links as $link) {
46                                 if($link['@attributes']['rel'] === 'alias') {
47                                         if(strpos($link['@attributes']['href'],'@') === false) {
48                                                 if(isset($profile)) {
49                                                         if($link['@attributes']['href'] !== $profile)
50                                                                 $alias = unamp($link['@attributes']['href']);
51                                                 }
52                                                 else
53                                                         $profile = unamp($link['@attributes']['href']);
54                                         }
55                                 }
56                         }
57                 }
58                 else {
59                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
60                                 $email_conversant = true;
61                         }
62                 }
63         }       
64
65         // If we find a DFRN site, send our subscriber to the other person's
66         // dfrn_request page and all the other details will get sorted.
67
68         if(strlen($dfrn)) {
69                 $ret = scrape_dfrn($dfrn);
70                 if(is_array($ret) && x($ret,'dfrn-request')) {
71                         if(strlen($a->path))
72                                 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
73                         else
74                                 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
75  
76                         goaway($ret['dfrn-request'] . "&addr=$myaddr");
77                 
78                         // NOTREACHED
79                 }
80         }
81
82         $network  = 'stat';
83         $priority = 0;
84
85         if($hcard) {
86                 $vcard = scrape_vcard($hcard);
87
88                 // Google doesn't use absolute url in profile photos
89
90                 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
91                         $h = @parse_url($hcard);
92                         if($h)
93                                 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
94                 }
95                 
96                 logger('mod_follow: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
97         }
98
99         if(! $profile) {
100                 if($diaspora)
101                         $profile = $hcard;
102                 else
103                         $profile = $url;
104         }
105
106         if(! x($vcard,'fn'))
107                 if(x($vcard,'nick'))
108                         $vcard['fn'] = $vcard['nick'];
109
110         if((! isset($vcard)) && (! $poll)) {
111
112                 $ret = scrape_feed($url);
113                 logger('mod_follow: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
114                 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
115                         $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
116                         $vcard = array();
117                                 if(x($ret,'photo'))
118                                         $vcard['photo'] = $ret['photo'];
119                         require_once('simplepie/simplepie.inc');
120                     $feed = new SimplePie();
121                         $xml = fetch_url($poll);
122
123                 $feed->set_raw_data($xml);
124
125                     $feed->init();
126
127                         if(! x($vcard,'photo'))
128                                 $vcard['photo'] = $feed->get_image_url();
129                         $author = $feed->get_author();
130                         if($author) {                   
131                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
132                                 if(! $vcard['fn'])
133                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
134                                 if(strpos($vcard['fn'],'@') !== false)
135                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
136                                 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
137                                 if(strpos($vcard['nick'],' '))
138                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
139                                 $email = unxmlify($author->get_email());
140                         }
141                         else {
142                                 $item = $feed->get_item(0);
143                                 if($item) {
144                                         $author = $item->get_author();
145                                         if($author) {                   
146                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
147                                                 if(! $vcard['fn'])
148                                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
149                                                 if(strpos($vcard['fn'],'@') !== false)
150                                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
151                                                 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
152                                                 if(strpos($vcard['nick'],' '))
153                                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
154                                                 $email = unxmlify($author->get_email());
155                                         }
156                                         if(! $vcard['photo']) {
157                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
158                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
159                                                         $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
160                                         }
161                                 }
162                         }
163                         if((! $vcard['photo']) && strlen($email))
164                                 $vcard['photo'] = gravatar_img($email);
165                         if($poll === $profile)
166                                 $lnk = $feed->get_permalink();
167                         if(isset($lnk) && strlen($lnk))
168                                 $profile = $lnk;        
169                         if(! (x($vcard,'fn')))
170                                 $vcard['fn'] = notags($feed->get_title());
171                         if(! (x($vcard,'fn')))
172                                 $vcard['fn'] = notags($feed->get_description());
173                         $network = 'feed';
174                         $priority = 2;
175                 }
176         }
177
178         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
179
180         $vcard['fn'] = notags($vcard['fn']);
181         $vcard['nick'] = notags($vcard['nick']);
182
183         // do we have enough information?
184         
185         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
186                 notice( t('The profile address specified does not provide adequate information.') . EOL);
187                 goaway($_SESSION['return_url']);
188         }
189
190
191         if(! $notify) {
192                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
193         }
194
195         if(! x($vcard,'photo'))
196                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
197
198         // check if we already have a contact
199         // the poll url is more reliable than the profile url, as we may have
200         // indirect links or webfinger links
201
202         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
203                 intval(local_user()),
204                 dbesc($poll)
205         );                      
206
207         if(count($r)) {
208                 // update contact
209                 if($r[0]['rel'] == REL_VIP) {
210                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
211                                 intval(REL_BUD),
212                                 intval($r[0]['id']),
213                                 intval(local_user())
214                         );
215                 }
216         }
217         else {
218                 // create contact record 
219                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
220                         `blocked`, `readonly`, `pending` )
221                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
222                         intval(local_user()),
223                         dbesc(datetime_convert()),
224                         dbesc($profile),
225                         dbesc($alias),
226                         dbesc($notify),
227                         dbesc($poll),
228                         dbesc($vcard['fn']),
229                         dbesc($vcard['nick']),
230                         dbesc($vcard['photo']),
231                         dbesc($network),
232                         intval(REL_FAN),
233                         intval($priority)
234                 );
235         }
236
237         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
238                 dbesc($profile),
239                 intval(local_user())
240         );
241
242         if(! count($r)) {
243                 notice( t('Unable to retrieve contact information.') . EOL);
244                 goaway($_SESSION['return_url']);
245                 // NOTREACHED
246         }
247
248         $contact = $r[0];
249         $contact_id  = $r[0]['id'];
250
251         require_once("Photo.php");
252
253         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
254
255         $r = q("UPDATE `contact` SET `photo` = '%s', 
256                         `thumb` = '%s',
257                         `micro` = '%s', 
258                         `name-date` = '%s', 
259                         `uri-date` = '%s', 
260                         `avatar-date` = '%s'
261                         WHERE `id` = %d LIMIT 1
262                 ",
263                         dbesc($photos[0]),
264                         dbesc($photos[1]),
265                         dbesc($photos[2]),
266                         dbesc(datetime_convert()),
267                         dbesc(datetime_convert()),
268                         dbesc(datetime_convert()),
269                         intval($contact_id)
270                 );                      
271
272
273         // pull feed and consume it, which should subscribe to the hub.
274
275         proc_run('php',"include/poller.php","$contact_id");
276
277         // create a follow slap
278
279         $tpl = load_view_file('view/follow_slap.tpl');
280         $slap = replace_macros($tpl, array(
281                 '$name' => $a->user['username'],
282                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
283                 '$photo' => $a->contact['photo'],
284                 '$thumb' => $a->contact['thumb'],
285                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
286                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
287                 '$title' => '',
288                 '$type' => 'text',
289                 '$content' => t('following'),
290                 '$nick' => $a->user['nickname'],
291                 '$verb' => ACTIVITY_FOLLOW,
292                 '$ostat_follow' => ''
293         ));
294
295         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
296                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
297                         intval(local_user())
298         );
299
300
301         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
302                 require_once('include/salmon.php');
303                 slapper($r[0],$contact['notify'],$slap);
304         }
305
306         goaway($_SESSION['return_url']);
307         // NOTREACHED
308 }