]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
48ad667476c788378b3326b1e8f7fd05f736b37a
[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
97         if(! $profile) {
98                 if($diaspora)
99                         $profile = $hcard;
100                 else
101                         $profile = $url;
102         }
103
104         if(! x($vcard,'fn'))
105                 if(x($vcard,'nick'))
106                         $vcard['fn'] = $vcard['nick'];
107
108         if((! isset($vcard)) && (! $poll)) {
109
110                 $ret = scrape_feed($url);
111                 logger('mod_follow: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
112                 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
113                         $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
114                         $vcard = array();
115                                 if(x($ret,'photo'))
116                                         $vcard['photo'] = $ret['photo'];
117                         require_once('simplepie/simplepie.inc');
118                     $feed = new SimplePie();
119                         $xml = fetch_url($poll);
120
121                 $feed->set_raw_data($xml);
122
123                     $feed->init();
124
125                         if(! x($vcard,'photo'))
126                                 $vcard['photo'] = $feed->get_image_url();
127                         $author = $feed->get_author();
128                         if($author) {                   
129                                 $vcard['fn'] = unxmlify(trim($author->get_name()));
130                                 if(! $vcard['fn'])
131                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
132                                 if(strpos($vcard['fn'],'@') !== false)
133                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
134                                 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
135                                 if(strpos($vcard['nick'],' '))
136                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
137                                 $email = unxmlify($author->get_email());
138                         }
139                         else {
140                                 $item = $feed->get_item(0);
141                                 if($item) {
142                                         $author = $item->get_author();
143                                         if($author) {                   
144                                                 $vcard['fn'] = trim(unxmlify($author->get_name()));
145                                                 if(! $vcard['fn'])
146                                                         $vcard['fn'] = trim(unxmlify($author->get_email()));
147                                                 if(strpos($vcard['fn'],'@') !== false)
148                                                         $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
149                                                 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
150                                                 if(strpos($vcard['nick'],' '))
151                                                         $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
152                                                 $email = unxmlify($author->get_email());
153                                         }
154                                         if(! $vcard['photo']) {
155                                                 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
156                                                 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
157                                                         $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
158                                         }
159                                 }
160                         }
161                         if((! $vcard['photo']) && strlen($email))
162                                 $vcard['photo'] = gravatar_img($email);
163                         if($poll === $profile)
164                                 $lnk = $feed->get_permalink();
165                         if(isset($lnk) && strlen($lnk))
166                                 $profile = $lnk;        
167                         if(! (x($vcard,'fn')))
168                                 $vcard['fn'] = notags($feed->get_title());
169                         if(! (x($vcard,'fn')))
170                                 $vcard['fn'] = notags($feed->get_description());
171                         $network = 'feed';
172                         $priority = 2;
173                 }
174         }
175
176         logger('follow: poll=' . $poll . ' notify=' . $notify . ' profile=' . $profile . ' vcard=' . print_r($vcard,true));
177
178         $vcard['fn'] = notags($vcard['fn']);
179         $vcard['nick'] = notags($vcard['nick']);
180
181         // do we have enough information?
182         
183         if(! ((x($vcard['fn'])) && ($poll) && ($profile))) {
184                 notice( t('The profile address specified does not provide adequate information.') . EOL);
185                 goaway($_SESSION['return_url']);
186         }
187
188
189         if(! $notify) {
190                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
191         }
192
193         if(! x($vcard,'photo'))
194                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
195
196         // check if we already have a contact
197         // the poll url is more reliable than the profile url, as we may have
198         // indirect links or webfinger links
199
200         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
201                 intval(local_user()),
202                 dbesc($poll)
203         );                      
204
205         if(count($r)) {
206                 // update contact
207                 if($r[0]['rel'] == REL_VIP) {
208                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
209                                 intval(REL_BUD),
210                                 intval($r[0]['id']),
211                                 intval(local_user())
212                         );
213                 }
214         }
215         else {
216                 // create contact record 
217                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
218                         `blocked`, `readonly`, `pending` )
219                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 0, 0, 0 ) ",
220                         intval(local_user()),
221                         dbesc(datetime_convert()),
222                         dbesc($profile),
223                         dbesc($alias),
224                         dbesc($notify),
225                         dbesc($poll),
226                         dbesc($vcard['fn']),
227                         dbesc($vcard['nick']),
228                         dbesc($vcard['photo']),
229                         dbesc($network),
230                         intval(REL_FAN),
231                         intval($priority)
232                 );
233         }
234
235         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
236                 dbesc($profile),
237                 intval(local_user())
238         );
239
240         if(! count($r)) {
241                 notice( t('Unable to retrieve contact information.') . EOL);
242                 goaway($_SESSION['return_url']);
243                 // NOTREACHED
244         }
245
246         $contact = $r[0];
247         $contact_id  = $r[0]['id'];
248
249         require_once("Photo.php");
250
251         $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
252
253         $r = q("UPDATE `contact` SET `photo` = '%s', 
254                         `thumb` = '%s',
255                         `micro` = '%s', 
256                         `name-date` = '%s', 
257                         `uri-date` = '%s', 
258                         `avatar-date` = '%s'
259                         WHERE `id` = %d LIMIT 1
260                 ",
261                         dbesc($photos[0]),
262                         dbesc($photos[1]),
263                         dbesc($photos[2]),
264                         dbesc(datetime_convert()),
265                         dbesc(datetime_convert()),
266                         dbesc(datetime_convert()),
267                         intval($contact_id)
268                 );                      
269
270
271         // pull feed and consume it, which should subscribe to the hub.
272
273         proc_run('php',"include/poller.php","$contact_id");
274
275         // create a follow slap
276
277         $tpl = load_view_file('view/follow_slap.tpl');
278         $slap = replace_macros($tpl, array(
279                 '$name' => $a->user['username'],
280                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
281                 '$photo' => $a->contact['photo'],
282                 '$thumb' => $a->contact['thumb'],
283                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
284                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
285                 '$title' => '',
286                 '$type' => 'text',
287                 '$content' => t('following'),
288                 '$nick' => $a->user['nickname'],
289                 '$verb' => ACTIVITY_FOLLOW,
290                 '$ostat_follow' => ''
291         ));
292
293         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
294                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
295                         intval(local_user())
296         );
297
298
299         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
300                 require_once('include/salmon.php');
301                 slapper($r[0],$contact['notify'],$slap);
302         }
303
304         goaway($_SESSION['return_url']);
305         // NOTREACHED
306 }